callback is not called using moq + autofaq

泪湿孤枕 提交于 2021-01-29 05:09:32

问题


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);
        }

回答1:


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);
    }


来源:https://stackoverflow.com/questions/32209383/callback-is-not-called-using-moq-autofaq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!