简体   繁体   English

为什么在使用AutoMock.GetLoose()时断点永远不会被命中?

[英]Why does my breakpoint never get hit, when using AutoMock.GetLoose()?

Consider the following simple test: 考虑以下简单测试:

[Fact]
public void Should_Test_Something()
{
    using (var mock = AutoMock.GetLoose())
    {
        using (var workflow = mock.Create<IWorkflow>())
        {
            var result = workflow.DoSomething();
            // ...
        }
    }
}

When setting a breakpoint inside of DoSomething() Visual Studio will never break upon it. DoSomething()内设置断点时,Visual Studio将永不中断。 Why is that? 这是为什么? I can step through the test without any issues. 我可以顺利完成测试,没有任何问题。

public interface IWorkflow
{
    bool DoSomething();
}

public class Workflow : IWorkflow
{
    public Workflow( // Some long list of dependencies...)

    public bool DoSomething()
    {
        // I do something, a breakpoint set here does never get hit
    }
}

When setting a breakpoint inside of DoSomething() Visual Studio will never break upon it. 在DoSomething()内设置断点时,Visual Studio将永不中断。 Why is that? 这是为什么? I can step through the test without any issues. 我可以顺利完成测试,没有任何问题。

Because the interface is being mock and used. 因为该接口正在被模拟和使用。 not the actual class implementation. 不是实际的类实现。

That is the whole point of mocking the interface to begin with. 这就是模拟界面的全部要点。 So that the actual class is not used. 这样就不会使用实际的类。 But rather the mock of the interface. 而是界面的模拟。

how can I test my method DoSomething() in an isolated way, without having to supply all the dependencies? 如何以隔离的方式测试我的方法DoSomething(),而不必提供所有依赖项?

You will need to mock all the dependencies and initialize the actual class with those dependencies. 您将需要模拟所有依赖关系,并使用这些依赖关系初始化实际的类。

[Fact]
public void Should_Test_Something() {
    using (var mock = AutoMock.GetLoose()) {
        //Arrange
        IWorkflow workflow = mock.Create<Workflow>(); //<-- note asking for actual class

        //Act
        var result = workflow.DoSomething();

        //Assert
        // ...assert expected behavior            
    }
}

Provided all the dependencies can be created with out undesirable behavior the auto mock with create mocks of the dependencies and pass that to the class. 如果可以创建所有依赖关系而不会出现不良行为,则自动模拟将创建依赖关系的模拟并将其传递给类。

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

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