程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> JustMock Lite (Free Mocking Framework For .net),justmockmocking

JustMock Lite (Free Mocking Framework For .net),justmockmocking

編輯:C#入門知識

JustMock Lite (Free Mocking Framework For .net),justmockmocking


     2.   官網下載(官網不行點這裡)

     3.   幫助文檔

商業版和免費版區別概覽

     MockingContainer

測試類准備:一般來說也是業務類


 

    public class ClassUnderTest
    {
        private IFirstDependency firstDep;
        private ISecondDependency secondDep;

        public ClassUnderTest(IFirstDependency first, ISecondDependency second)
        {
            this.firstDep = first;
            this.secondDep = second;
        }

        public IList<object> CollectionMethod()
        {
            var firstCollection = firstDep.GetList();

            return firstCollection;
        }

        public string StringMethod()
        {
            var secondString = secondDep.GetString();

            return secondString;
        }
    }

    public interface IFirstDependency
    {
        IList<object> GetList();
    }

    public interface ISecondDependency
    {
        string GetString();
    }

  單元測試


 

        [TestMethod]
        public void ShouldMockDependenciesWithContainer()
        {
            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest. 
            // To instantiate the system uder test (the container) you should use the Instance property 
            // For example: container.Instance. 
            var container = new MockingContainer<ClassUnderTest>();

            string expectedString = "Test";

            // Arranging: When the GetString() method from the ISecondDependecy interface 
            //              is called from the container, it should return expectedString. 
            container.Arrange<ISecondDependency>(
               secondDep => secondDep.GetString()).Returns(expectedString);

            // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
            var actualString = container.Instance.StringMethod();

            // ASSERT
            Assert.AreEqual(expectedString, actualString);
        }

  需要說明的是MockingContainer構造函數有一個可選參數AutoMockSettings,其中AutoMockSettings最主要的一個作用的是當ClassUnderTest有多個構造函數時,指定合適的構造函數來實例化對象

     當業務類中有一個無參構造函數,一個有參構造函數,在沒有設置AutoMockSettings的情況下會默認執行無參構造函數,

    可以像下面這樣來指定需要的構造函數

        AutoMockSettings settings = new AutoMockSettings
            {
                ConstructorArgTypes = new Type[]{
                  typeof(IFirstDependency),typeof(ISecondDependency)
                }
            };

            // ARRANGE
            // Creating a MockingContainer of ClassUnderTest. 
            // To instantiate the system uder test (the container) you should use the Instance property 
            // For example: container.Instance. 
            var container = new MockingContainer<ClassUnderTest>(settings);

  

     

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved