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);