简体   繁体   English

未调用 xUnit 设置函数

[英]xUnit Setup function not being called

I have a very basic unit test.我有一个非常基本的单元测试。 For some reason the test passed only the first time but all subsequent calls have failed.出于某种原因,测试只通过了第一次,但所有后续调用都失败了。 Here is the test code:下面是测试代码:

private Mock<IMyInterface> _mockObject;
public Test() {
   _mockObject = new Mock<IMyInterface>();
}

[Fact]
public void ItReturnsTheRightThing() {
   _mockObject.SetUp(o => o.FunctionIWantToMock()).Returns(new SpecialObject(true));
   var classIWantToTest = new ClassIWantToTest(_mockObject.Object);
   classIWantToTest.RunCode();
   ...
}

The interface:界面:

internal interface IMyInterface {
    SpecialObject FunctionIWantToMock();
}

The production code:生产代码:

public class ClassIWantToTest {
    IMyInterface _client;
    internal ClassIWantToTest(IMyInterface client) {
        _client = client;
    }
    public bool RunCode() {
        SpecialObject so = _client.FunctionIWantToMock();
        return so.Value; // This line is the problem
        ...
    }
}

The problem is that when I run my test, my mock function isn't returning what I setup in the SetUp function, it's not returning the SpecialObject(true) but rather null .问题是,当我运行我的测试时,我的模拟函数没有返回我在 SetUp 函数中设置的内容,它没有返回SpecialObject(true)而是null I then get a null reference exception.然后我得到一个空引用异常。 When I debug the test, I can't step into the _client.FunctionIWantToMock call, it just immediately steps to the next line and populates so with null.当我调试测试,我不能踏进_client.FunctionIWantToMock调用,它只是立即几步到下一行,并填充so用空。

Since this doesn't make a whole lot of sense, I would try something like this:由于这没有多大意义,我会尝试这样的事情:

[Fact]
public void ItReturnsTheRightThing() {
    _mockObject = new Mock<IMyInterface>();
   _mockObject.SetUp(o => o.FunctionIWantToMock()).Returns(new SpecialObject(true));
   var classIWantToTest = new ClassIWantToTest(_mockObject.Object);
   classIWantToTest.RunCode();
  ...
}

Try this and if it works, something interferes with the mock object in between试试这个,如果它有效,一些东西会干扰中间的模拟对象

A coworker helped me figure out the issue.一位同事帮我解决了这个问题。 It is also my fault that I obfuscated too much of the original code when writing up this question.我在写这个问题时混淆了太多的原始代码也是我的错。

The problem was that I was passing explicit parameters to the o.FunctionIWantToMock() method during the setup, that did not match the explicit parameters passed to the function in the business logic.问题是我在设置期间将显式参数传递给o.FunctionIWantToMock()方法,这与传递给业务逻辑中函数的显式参数不匹配。 So the actual test code was o.FunctionIWantToMock("foo", "bar") .所以实际的测试代码是o.FunctionIWantToMock("foo", "bar") In my business logic, the actual function is called with _client.FunctionIWantToMock("Foo", "Bar") .在我的业务逻辑中,实际函数是用_client.FunctionIWantToMock("Foo", "Bar")调用的。 So because the parameters didn't match ("Foo" vs "foo", "Bar" vs "bar") the SetUp wasnt actually setting up my actual code.因此,由于参数不匹配(“Foo”与“foo”、“Bar”与“bar”),SetUp 实际上并未设置我的实际代码。 I changed the setUp function to我将设置功能更改为

o.FunctionIWantToMock(It.IsAny<string>(), It.IsAny<string>())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM