【问题标题】:Unit Testing Moq not passing through method when virtual单元测试起订量在虚拟时不通过方法
【发布时间】:2011-08-12 02:29:21
【问题描述】:

你好,说我有一些代码,例如:

public class Class1
    {
        public  int MyMethod()
        {
            return MyOtherMethod();
        }

        public virtual int MyOtherMethod()
        {
            return 1;
        }
    }

好的,这并没有太大的相关性,但这只是一个简单的例子。

然后我创建一个新测试:

[TestMethod]
        public void TestMethod1()
        {
            var t = new Mock<Class1>();

            var w = t.Object.MyMethod();
        }

有人可以告诉我为什么代码在未指定为虚拟时通过调用的方法 MyOtherMethod 运行,但是当您将其设为虚拟时,测试代码拒绝通过该方法?

【问题讨论】:

    标签: unit-testing mocking moq


    【解决方案1】:

    您应该在调用 MyMethod 之前设置虚拟方法:

    t.Setup(c => c.MyOtherMethod()).Return(1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多