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

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

考虑以下简单测试:

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

DoSomething() 内部设置断点时,Visual Studio 将永远不会在其上中断。这是为什么?我可以毫无问题地逐步完成测试。

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. Why is that? I can step through the test without any issues.

因为接口正在被模拟和使用。不是实际的 class 实现。

这就是模拟界面的全部意义所在。这样就不会使用实际的 class。而是界面的模拟。

how can I test my method DoSomething() in an isolated way, without having to supply all the dependencies?

您将需要模拟所有依赖项并使用这些依赖项初始化实际 class。

[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            
    }
}

如果可以创建所有依赖项而不会出现不良行为,则自动模拟会创建依赖项的模拟并将其传递给 class。