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

Source: Internet
Author: User

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

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.