简体   繁体   English

为什么moq声称我的mock的属性setter永远不会被调用,即使代码调用它?

[英]Why is moq claiming my mock's property setter is never being called even though the code is calling it?

I have the following unit test: 我有以下单元测试:

[TestClass]
public class DirectoryWatcherTests
{
    private AutoMoqer _mocker;
    private DirectoryWatcher _instance;

    [TestInitialize]
    public void Setup()
    {
        _mocker = new AutoMoqer();
        _instance = _mocker.Create<DirectoryWatcher>();
    }

    [TestMethod]
    public void Watcher_Gets_Path_Set_Same_As_Begin_Path_Parameter()
    {
        const string path = @"C:\test";
        _instance.Begin(path);

        _mocker.GetMock<FileSystemWatcherBase>()
               .VerifySet(x => x.Path = path);
    }
}

The code i wrote to get this to pass was: 我写的代码是为了通过:

public class DirectoryWatcher
{
    private readonly FileSystemWatcherBase _fileSystemWatcher;

    public DirectoryWatcher(FileSystemWatcherBase fileSystemWatcher)
    {
        _fileSystemWatcher = fileSystemWatcher;
    }

    public void Begin(string path)
    {
        if (string.IsNullOrWhiteSpace(path))
            throw new ArgumentException("FileSystemWatcher passed in without a valid path already set");

        _fileSystemWatcher.Path = path;
    }
}

However, the VerifySet fails with: 但是, VerifySet失败了:

Expected invocation on the mock at least once, but was never performed: x => x.Path = "C:\\test" 模拟上的预期调用至少一次,但从未执行过:x => x.Path =“C:\\ test”

Why is it claiming the setter is never being called? 为什么它声称这个二传手从未被召唤过? If it helps at all FileSystemWatcherBase is an abstract class. 如果它有帮助, FileSystemWatcherBase是一个抽象类。

Thanks to Eugene I have figured out that this seems to be an issue with Automoq and its compatibility with the latest version of Unity. 感谢Eugene,我发现这似乎是Automoq的一个问题,以及它与最新版Unity的兼容性。 I created the following tests to prove that it's an Automoq issue and not a Moq issue: 我创建了以下测试来证明它是一个Automoq问题而不是Moq问题:

    [TestMethod]
    public void Test()
    {
        const string path = @"C:\test";
        var watcherMock = new Mock<FileSystemWatcherBase>();
        watcherMock.Object.Path = path;
        watcherMock.VerifySet(x => x.Path = path);
    }

    [TestMethod]
    public void Test2()
    {
        const string path = @"C:\test";
        var mocker = new AutoMoqer();
        var instance = mocker.Create<Tester>();
        var watcherMock = mocker.GetMock<AbstractTest>();
        watcherMock.Object.Path = path;
        watcherMock.VerifySet(x => x.Path = path);
    }

    [TestMethod]
    public void Test3()
    {
        const string path = @"C:\test";
        var mocker = new AutoMoqer();
        var instance = mocker.Create<Tester>();
        var watcherMock = mocker.GetMock<AbstractTest>();
        instance.Run(path);
        watcherMock.VerifySet(x => x.Path = path);
    }

    [TestMethod]
    public void Test4()
    {
        const string path = @"C:\test";
        var testMock = _mocker.GetMock<AbstractTest>();
        var tester = new Tester(testMock.Object);
        tester.Run(path);

        testMock.VerifySet(x => x.Path = path);
    }

    public abstract class AbstractTest
    {
        public abstract string Path { get; set; }
    }

    public class Tester
    {
        private readonly AbstractTest _test;

        public Tester(AbstractTest test)
        {
            _test = test;
        }

        public void Run(string path)
        {
            _test.Path = path;
        }
    }

Tests 1, 2 and 4 pass while 3 fails. 测试1,2和4通过而3失败。 I was able to find a work around the issue via the following test case: 我能够通过以下测试用例找到有关该问题的工作:

    [TestMethod]
    public void Test5()
    {
        const string path = @"C:\test";
        var mocker = new AutoMoqer();
        var watcherMock = mocker.GetMock<AbstractTest>();
        var instance = mocker.Create<Tester>();

        instance.Run(path);
        watcherMock.VerifySet(x => x.Path = path);
    }

Essentially having Automoq get me the mock prior to creating the class I'm trying to test allows the verify to work. 基本上让Automoq在创建我正在尝试测试的类之前让我获得模拟允许验证工作。 This leads me to believe that Automoq does not realize a moq was already created for the tested class and thus a call to GetMock<T> creates a new one. 这让我相信Automoq没有意识到已经为测试类创建了一个moq,因此对GetMock<T>的调用会创建一个新的。

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

相关问题 为什么JustMock声称永远不会调用我的模拟方法? - Why is JustMock claiming my mocked method is never getting called? 为什么Moq会在“模拟至少一次”上投掷“预期的调用”。它被设置为一次,即使它被设置为null? - Why Moq is throwing “expected Invocation on the mock at least once”. Where as it is being set once,even though it is being set to null? Moq-验证返回零,即使我可以看到方法被调用 - Moq - verify returns zero even though I can see the methods being called 为什么在DataContract上调用Dispose,即使该服务仍然引用它? - Why Dispose is being called on DataContract even though the service still refers to it? 拥有受保护的二传手的Moq财产 - Moq property with protected setter 即使将调用方法,起订量验证方法也会失败 - Moq verify method fails even though method will be called 如果我的对象从未被构造,为什么要调用Dispose? - Why is Dispose being called, if my object was never constructed? 为什么永远不会调用Custom DataAnnotationsModelMetadataProvider的CreateMetadata? - Why is Custom DataAnnotationsModelMetadataProvider's CreateMetadata never being called? 验证错误阻止调用属性设置器 - Validation errors prevent the property setter being called 属性值更改而不调用 setter - Property value changes without setter being called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM