JustMock Lite (Free Mocking Framework For .net)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > JustMock Lite (Free Mocking Framework For .net)

JustMock Lite (Free Mocking Framework For .net)

 2015/4/25 23:07:01  麻将我会  程序员俱乐部  我要评论(0)
  • 摘要:通过Nuget安装2.官网下载(官网不行点这里)3.帮助文档商业版和免费版区别概览MockingContainer测试类准备:一般来说也是业务类publicclassClassUnderTest{privateIFirstDependencyfirstDep;privateISecondDependencysecondDep;publicClassUnderTest(IFirstDependencyfirst,ISecondDependencysecond){this
  • 标签:.net for Framework net
  1. 通过 Nuget 安装

     2.   官网下载(官网不行点这里)

     3.   帮助文档

商业版和免费版区别概览

     MockingContainer

测试类准备:一般来说也是业务类


 

class="brush:csharp;gutter:true;">    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);

  

     

发表评论
用户名: 匿名