猿问

使用接口在 Moq 中模拟泛型方法

我正在尝试为以下通用接口设置最小起算,但遇到异常


 public interface IReadAccess<TEntity>

 {

     Task<IEnumerable<TEntity>> GetAll();

 }


var m = new Mock<IReadAccess<Foo>>(MockBehavior.Strict);


m.Setup(p => p.GetAll()).ReturnsAsync(new List<Foo>());


m.VerifyAll();

获取波纹管异常


Moq.MockException

  HResult=0x80131500

  Message=The following setups on mock

    'Mock<EPIC.CrossCutting.Interfaces.DAL.Framework.IReadAccess<EPIC.CrossCutting.DTOs.Data.Announcement.AnnouncementCrosscutDTO>:00000002>' 

  were not matched:

IReadAccess<AnnouncementCrosscutDTO> p => p.GetAll()


  Source=Moq

  StackTrace:

   at Moq.Mock.VerifyAll()

   at EPIC.Tests.Business.Rules.Announcements.AnnouncementPlanning.CrosscutsProgrammaticActivitiesValidationRuleServiceTests.<ExecuteSuccessTest>d__5.MoveNext() 

in D:\dev\main\Tests\EPIC.Tests.Business.Rules\Announcements\AnnouncementPlanning\CrosscutsProgrammaticActivitiesValidationRuleServiceTests.cs:line 108



慕姐8265434
浏览 129回答 2
2回答

收到一只叮咚

您的测试正确失败,因为您尝试验证 GetAll() 是否被调用,即使您实际上并未调用它。如果在测试或正在测试的代码中调用该方法,它将通过。[Fact]public async Task Test1(){&nbsp; &nbsp; var m = new Mock<IReadAccess<Foo>>(MockBehavior.Strict);&nbsp; &nbsp; m.Setup(p => p.GetAll()).ReturnsAsync(new List<Foo>());&nbsp; &nbsp; var result = await m.Object.GetAll();&nbsp; &nbsp; m.VerifyAll();}线索在您的错误消息中:消息=模拟上的以下设置....不匹配: IReadAccess p => p.GetAll()

守候你守候我

这是预期的工作,但我有类似的方法public interface IReadAccess<TEntity>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp; Task<IEnumerable<TEntity>> GetAll();&nbsp; &nbsp;&nbsp; &nbsp; Task<IEnumerable<TEntity>> Find(FormattableString whereClause, object whereClauseObject);}&nbsp;并尝试设置var m = new Mock<IReadAccess<Foo>>(MockBehavior.Strict);m.Setup(x => x.Find($"ID = @ID", new { ID = 5 })).ReturnsAsync(new List<Foo>());var result = await m.Object.Find($"ID= @ID", new { ID = 5 });m.VerifyAll();在对现有代码进行了一些更改后,现在安装程序工作正常,但是当它从测试,更新的代码调用时,实际服务会出现错误var test = new Test {ID = 5};object whereClause = new { ID = test.ID };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormattableString formattableString = $"ID = @ID";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.Setup(x => x.Find(formattableString, whereClause)).ReturnsAsync(new List<Foo>());&nbsp;var ruleServiceOutput = await this.testValidationRuleService.ExecuteAsync(test);实际代码public async Task<IRuleServiceOutput<bool>> ExecuteAsync(Test test)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var errors = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object whereClause = new { ID = test.ID };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormattableString formattableString = $"ID = @ID";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var output = (await m.Find(formattableString, whereClause)).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new RuleServiceOutput<bool>(output.Errors.IsEmpty(), output.Errors);&nbsp; &nbsp; &nbsp; &nbsp; }'IReadAccess'1.Find(ID = @ID, { ID = 5 }) 调用失败,模拟行为严格。模拟上的所有调用都必须具有相应的设置。设置 MockBehavior 后。默认所有正常工作的预期:)
随时随地看视频慕课网APP
我要回答