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.
原文地址:https://www.cnblogs.com/wgscd/p/14867127.html