I am trying to setup moq for the following generic interface but getting exception
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();
Getting bellow exception
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
Your test is failing correctly because you try to verify that GetAll() was called even though you haven't actually called it.
It'll pass if you call the method in your test or in the code your are testing.
[Fact]
public async Task Test1()
{
var m = new Mock<IReadAccess<Foo>>(MockBehavior.Strict);
m.Setup(p => p.GetAll()).ReturnsAsync(new List<Foo>());
var result = await m.Object.GetAll();
m.VerifyAll();
}
The clue was in your error message: Message=The following setups on mock .... were not matched: IReadAccess p => p.GetAll()
Thanks Connell , this is working expected but I have simillar method
public interface IReadAccess<TEntity>
{
Task<IEnumerable<TEntity>> GetAll();
Task<IEnumerable<TEntity>> Find(FormattableString whereClause, object whereClauseObject);
}
and trying to setup
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();
after made some changes on the existing code , now setup is working fine but getting error on actual service when it is calling for from test, updated code
var test = new Test {ID = 5};
object whereClause = new { ID = test.ID };
FormattableString formattableString = $"ID = @ID";
m.Setup(x => x.Find(formattableString, whereClause)).ReturnsAsync(new List<Foo>());
var ruleServiceOutput = await this.testValidationRuleService.ExecuteAsync(test);
Actual Code
public async Task<IRuleServiceOutput<bool>> ExecuteAsync(Test test)
{
var errors = new List<string>();
object whereClause = new { ID = test.ID };
FormattableString formattableString = $"ID = @ID";
var output = (await m.Find(formattableString, whereClause)).ToArray();
return new RuleServiceOutput<bool>(output.Errors.IsEmpty(), output.Errors);
}
'IReadAccess`1.Find(ID = @ID, { ID = 5 }) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.'
After setting MockBehavior.Default everything working expected :)
User contributions licensed under CC BY-SA 3.0