-2

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
3
  • Can you fix your code snippet so that we can see how your mock was set up?
    – Kenneth K.
    Feb 11, 2019 at 19:08
  • 1
    Is there some code between Setup and VerifyAll that actually calls the GetAllmethod?
    – Markus
    Feb 11, 2019 at 19:10
  • Please read minimal reproducible example guidance and edit post to show code that matches and reproduces your error message. Currently code is clearly some simplification that can't result in error shown. Feb 11, 2019 at 19:14

2 Answers 2

1

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()

1
  • Thanks Connell , this is working expected but I have simillar method public interface IReadAccess<TEntity> { Task<IEnumerable<TEntity>> GetAll(); }
    – Azad
    Feb 11, 2019 at 22:59
0

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 :)

1
  • Try setting up the mock like this m.Setup(x => x.Find(It.IsAny<FormattableString>(), It.IsAny<object>())).ReturnsAsync(new List<Foo>()); Feb 12, 2019 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.