回调不是使用moq + autofaq调用的

本文关键字:autofaq 调用 moq 回调 | 更新日期: 2023-09-27 18:09:42

我有一个使用moq模拟对象的单元测试,测试工作正常,现在我想使用autoface +moq,但我有一些问题。下面是测试:

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

调用sut。CheckInStorage返回null,并且变量numKeys没有更新到正确的值。这个测试只使用mock就可以很好地工作,所以我想我遗漏了一些如何使用autoMock配置mock的内容。我在哪里可以找到更多的信息?

:

经过几次测试,我找到了解决方案

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

回调不是使用moq + autofaq调用的

经过几次测试,我找到了解决方案

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