zoukankan      html  css  js  c++  java
  • C# Moq

    测试:

    public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                Mock<TestClass> moc = new Mock<TestClass>();
                moc.Setup(x => x.GetItem(It.IsAny<int>())).Returns("123");
                moc.Setup(x => x.name).Returns("wgscd");
                TestClass t =moc.Object;
                Assert.AreEqual("123", t.GetItem(222));
                Assert.AreEqual("wgscd", t.name);
    
    
            }
        }
    
        public   class TestClass
        {
            public string GetItem(int i) {
                return "";
            }
            public string name   { get;set;  }
    
    
        }
    

      

    上面代码会错误出现:

    Moq - Non-overridable members may not be used in setup / verification expressions

    修改后OK:

    public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                Mock<TestClass> moc = new Mock<TestClass>();
                moc.Setup(x => x.GetItem(It.IsAny<int>())).Returns("123");
                moc.Setup(x => x.name).Returns("wgscd");
                TestClass t =moc.Object;
                Assert.AreEqual("123", t.GetItem(222));
                Assert.AreEqual("wgscd", t.name);
    
    
            }
        }
    
        public   class TestClass
        {
            public virtual string GetItem(int i) {
                return "";
            }
            public virtual string name   { get;set;  }
    
    
        }
    

      

    原因是:moc 的对象的成员或字段必须可以 override 的。

    Moq creates an implementation of the mocked type. If the type is an interface, it creates a class that implements the interface. If the type is a class, it creates an inherited class, and the members of that inherited class call the base class. But in order to do that it has to override the members. If a class has members that can't be overridden (they aren't virtual, abstract) then Moq can't override them to add its own behaviors.

    In this case there's no need to mock PagingOptions because it's easy to use a real one. Instead of this:

    var mockPagingOptions = new Mock<PagingOptions>();
            mockPagingOptions.Setup(po => po.Limit).Returns(25);
            mockPagingOptions.Setup(po => po.Offset).Returns(0);
    

    Do this:

    var pagingOptions = new PagingOptions { Limit = 25, Offset = 0 };
    

    How do we determine whether or not to mock something? Generally speaking, we mock something if we don't want to include the concrete runtime implementation in our test. We want to test one class not both at the same time.

    But in this case PagingOptions is just a class that holds some data. There's really no point in mocking it. It's just as easy to use the real thing.

    另外有个回答的还提到测试私有字段的方法(就是通过反射设置私有字段值):

    n case you reached this question based on the original title Non-overridable members may not be used in setup / verification expressions and none of the other answers have helped you may want to see if reflection can satisfy your test needs.

    Suppose you have a class Foo with a property defined as public int I { get; private set; }

    If you try the various methods in the answers here few of them will work for this scenario. However you can use .net reflection to setup a value of an instance variable and still keep fairly good refactoring support in the code.

    Here is a snippet that sets a property with a private setter:

    var foo = new Foo();
    var I = foo.GetType().GetProperty(nameof(Foo.I), BindFlags.Public | BindingFlag.Instance);
    I.SetValue(foo, 8675309);
    fffffffffffffffff
    test red font.
  • 相关阅读:
    Oracle 学习笔记 12 -- 序列、索引、同义词
    解决:设置中打开蓝牙,測试机不会自己主动搜索设备
    mysql 数据库查询最后两条数据
    jQuery中this与$(this)的差别
    Apache http server linux 安装过程说明
    Swift学习——Swift基础具体解释(一)
    CloudFoundry V2 单机版离线安装(伪离线安装)
    CF1119F Niyaz and Small Degrees
    2019-8-31-C#-如何引用-WshShell-类
    2019-8-31-C#-如何引用-WshShell-类
  • 原文地址:https://www.cnblogs.com/wgscd/p/14867127.html
Copyright © 2011-2022 走看看