0

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

Why is JustMock not detecting the occurrence correctly? This is with the latest JustMock lite Nuget package (2014.1.1317.4)

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • I assume `_trackedFileStore` is an injected dependency? Can we see your setup for the `_mockContainer`? – Simon Whitehead Feb 13 '14 at 23:44
  • Yep sorry, I added the full test class and implementation class. – KallDrexx Feb 13 '14 at 23:46
  • You never pass a mock of `ITrackedFileStore` into the `DirectoryCrawler` mock. Therefore, the mock cannot verify that it was called. It may also have something to do with the fact you're mocking a concrete object.. which we don't generally do (and I'm more familiar with Moq too). – Simon Whitehead Feb 13 '14 at 23:50
  • This was supposed to utilize auto-mocking to keep me from having to manually manage mocked objects that are irrelevant to the test at hand. Example I'm following: http://www.telerik.com/help/justmock/basic-usage-automocking.html – KallDrexx Feb 13 '14 at 23:51
  • Interesting.. didn't realize that (Moq over here). ANyway, according to that tutorial, you need to `Arrange` the `GetTrackedFilesInPath` method to specify `MustBeCalled()`. Looks to me like that is how you do it with auto mocking (based on that documentation). – Simon Whitehead Feb 13 '14 at 23:59
  • Good catch! I didn't notice that when I was going through it :(. If you put that up as an answer I'll mark it as such. Thanks. And yeah, automocking is the main reason I am using JustMock instead of Moq (I've always used moq in the past). – KallDrexx Feb 14 '14 at 00:08
  • Moq has a couple of auto mocking extensions that you can use. There's one on NuGet if I remember correctly called Moq.Automock. Glad I could help anyway :) I have added an answer.. because I'm a bit of a reputation whore :P – Simon Whitehead Feb 14 '14 at 00:09

1 Answers1

0

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().

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138