KallDrexx
KallDrexx

Reputation: 27803

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:

Expected invocation on the mock at least once, but was never performed: 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.

Upvotes: 3

Views: 601

Answers (1)

KallDrexx
KallDrexx

Reputation: 27803

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. I created the following tests to prove that it's an Automoq issue and not a Moq issue:

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

Upvotes: 3

Related Questions