【发布时间】:2021-03-19 00:35:56
【问题描述】:
我遇到了这个错误
[xUnit.net 00:00:03.08] Moq.MockException :
[xUnit.net 00:00:03.08] Expected invocation on the mock at least once, but was never performed: s => s.Create(ContactScore, It.IsAny<IDbConnection>())
[xUnit.net 00:00:03.08]
[xUnit.net 00:00:03.08] Performed invocations:
[xUnit.net 00:00:03.08]
[xUnit.net 00:00:03.08] Mock<ContactScoreRepo:1> (s):
[xUnit.net 00:00:03.08]
[xUnit.net 00:00:03.08] ContactScoreRepo.Create(ContactScore, NpgsqlConnection)
[xUnit.net 00:00:03.08]
我想我的问题是 NpgsqlConnection 不匹配 It.IsAny<IDbConnection>() 但我不确定,
我的验证
var scoreCreated = new ContactScore
{
CampaignId = 1,
TokenId = 1,
ContactId = 1,
Score = 1,
DebtorId = 1,
};
scoreRepo.Verify(s => s.Create(scoreCreated, It.IsAny<IDbConnection>()));
以及调用方法的实际代码
await ScoreRepo.Create(new ContactScore
{
CampaignId = (int)campaign.Id,
TokenId = token.Id,
ContactId = (int)contact.Id,
Score = req.Score,
DebtorId = req.DebtorId,
}, conn);
其中conn 是NpgsqlConnection
我对 Moq(以及一般的 C#)非常陌生,并且阅读了文档 https://github.com/Moq/moq4/wiki/Quickstart,但无法弄清楚我应该如何解决这个错误。我错过了什么?
【问题讨论】: