简体   繁体   中英

callback is not called using moq + autofaq

I have a unit test done using moq to mock the objects, and the test is working fine, and now I want to use autofac +moq, but I'm having a few problems. this is the test:

using (var mock = AutoMock.GetLoose())
{

    var issues = new List<Issue>();
    issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
    issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
    var numKeys = 0;

    mock.MockRepository.Create<IStorageService>()
        .Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(), 
                                               It.IsAny<string>(), 
                                               It.IsAny<IList<string>>()))
        .Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
        .Returns(issues);

    var sut = mock.Create<IssueReceiveService>();

    var check = await sut.CheckInStorage("org", "repo", issues);
    Assert.AreEqual(issues.Count, numKeys);
}

the call to sut.CheckInStorage return null, and the variable numKeys is not updated to the correct value. This test works fine using just moxk, so I suppose I'm missing something how to configure a mock with autoMock. Where can I find more informations?

UPDATE :

after a few more tests I found the solution

       using (var mock = AutoMock.GetLoose())
        {
            var issues = new List<Issue>();
            issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
            issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
            var numKeys = 0;

            mock.Mock<IStorageService>()
                .Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IList<string>>()))
                .Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
                .Returns(issues);

            var sut = mock.Create<IssueReceiveService>();

            var check = await sut.CheckInStorage("org", "repo", issues);
            Assert.AreEqual(issues.Count, numKeys);
        }

after a few more tests I found the solution

   using (var mock = AutoMock.GetLoose())
    {
        var issues = new List<Issue>();
        issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
        issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
        var numKeys = 0;

        mock.Mock<IStorageService>()
            .Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IList<string>>()))
            .Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
            .Returns(issues);

        var sut = mock.Create<IssueReceiveService>();

        var check = await sut.CheckInStorage("org", "repo", issues);
        Assert.AreEqual(issues.Count, numKeys);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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