JustMock Lite (Free Mocking Framework For. net), justmockmocking
2. Download from the official website (Click here if the official website is unavailable)
3. Help document
Overview of differences between commercial and free versions
MockingContainer
Test preparation: Generally, it is also a business type.
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(); }
Unit Test
[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); }
It must be noted thatThe MockingContainer constructor has an optional parameter AutoMockSettings. One of the most important functions of AutoMockSettings is to specify an appropriate constructor to instantiate an object when ClassUnderTest has multiple constructor functions.
When a service class has a no-argument constructor and a constructor with parameters, the system executes the no-argument constructor by default if AutoMockSettings is not set,
You can specify the required constructor as follows:
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);