[英]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.