简体   繁体   English

为什么JustMock声称永远不会调用我的模拟方法?

[英]Why is JustMock claiming my mocked method is never getting called?

I have the following code in my application: 我的应用程序中包含以下代码:

public class DirectoryCrawler
{
    private IPathWrap _path;
    private IDirectoryWrap _directory;
    private ITrackedFileStore _trackedFileStore;
    private IFileWrap _file;

    public DirectoryCrawler(IPathWrap path, ITrackedFileStore trackedFileStore, IDirectoryWrap directory, IFileWrap file)
    {
        _path = path;
        _trackedFileStore = trackedFileStore;
        _directory = directory;
        _file = file;
    }

    public void CheckDirectoryContents(string baseDirectory)
    {
        var trackedFiles = _trackedFileStore.GetTrackedFilesInPath(baseDirectory);
    }
}

I'm unit testing it via: 我正在通过以下方式进行单元测试:

[TestClass]
public class DirectoryCrawlerTests
{
    private MockingContainer<DirectoryCrawler> _mockContainer;

    [TestInitialize]
    public void Setup()
    {
        _mockContainer = new MockingContainer<DirectoryCrawler>();
    }

    [TestMethod]
    public void Requests_Tracked_Files_In_Path()
    {
        var instance = _mockContainer.Instance;
        instance.CheckDirectoryContents("C:\\Test");

        _mockContainer.Assert<ITrackedFileStore>(x => x.GetTrackedFilesInPath(Arg.IsAny<string>()), Occurs.Once());
    }
}

However, the assert is failing claiming Result Message: Occurrence expectation failed. Expected exactly 1 call. Calls so far: 0 但是,断言未能声明Result Message: Occurrence expectation failed. Expected exactly 1 call. Calls so far: 0 Result Message: Occurrence expectation failed. Expected exactly 1 call. Calls so far: 0

Why is JustMock not detecting the occurrence correctly? 为什么JustMock无法正确检测到发生? This is with the latest JustMock lite Nuget package (2014.1.1317.4) 这是最新的JustMock lite Nuget软件包(2014.1.1317.4)

As stated in the comments, call verification is different when you use AutoMocking. 如评论中所述,使用自动模拟时,呼叫验证是不同的。

You must Arrange the automocked dependency method to be called and specify it with MustBeCalled() . 您必须Arrange要调用的自动模拟依赖方法,并使用MustBeCalled()指定它。

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

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