问题
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