0

I am trying to Mock my repository layer and I have a method GetSelection(Expression<Func<T, Boolean>> where). I am using Ninjects MickingKernel with Moq to achieve this.

When I do the following, it is fine:

// Create instance of repository
var kernel = new MoqMockingKernel();
var templateRepoMock = kernel.GetMock<ITemplateRepo>();

// Setup mock
templateRepoMock.Setup(x=>x.GetSelection(y=>y.FieldName)
                .Returns(new List<Template>() {new Template { ... })); 
                // Lets pretend that FieldName is a bool!

// Call Service layer
var result = templateService.MyMethod();

// -> Service Layer method
public List<Template> MyMethod() 
{
    return _templateRepo.GetSelection(x=>x.FieldName);
}

But when I try and add an additional parameter within my expression I get an ArgumentNullExeption:

// Setup mock 
templateRepoMock.Setup(x=>x.GetSelection(y=>y.SomeOtherField.Id == 1 
                                         && y.FieldName)
                .Returns(new List<Template>() {new Template { ... }));

When I update my service to the following:

public List<Template> MyMethod(SomeObject myObject) 
{
    return _templateRepo.GetSelection(x=>x.SomeObject.Id == myObject.Id 
                                      && x.FieldName).ToList(); 
}

It appears to be fine if I update myObject.Id to 1.

Any ideas why this could be happening?

Matt
  • 2,691
  • 3
  • 22
  • 36
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 07 '14 at 12:10
  • I always think very carefully about the content of my posts - alas I forgot to omit this. – Matt Aug 07 '14 at 12:18
  • I copied and ran your code but I didn't see any exception. Where is the exception thrown? Can you include more code that might be relevant? What version of Moq are you using? Also, if you haven't already, try to reproduce it yourself in a separate project; maybe you'll get the same results as me. – Patrick Quirk Aug 07 '14 at 12:28
  • The exception is being thrown in the service layer - not within the test – Matt Aug 07 '14 at 12:29
  • Here are the versions I am using: – Matt Aug 07 '14 at 12:41
  • With your versions I still don't see an exception; you'll need to post a [SSCCE](http://sscce.org/) that reproduces it since I'm missing something that you have. Also, if the error is thrown in your service layer, then can you not just debug the test and break where it's being thrown and see what is null? – Patrick Quirk Aug 07 '14 at 13:18
  • Added more to the question to help explain my problem... – Matt Aug 07 '14 at 14:12
  • I have also added an example on GitHub https://github.com/mhooper/MockingKernel.Moq.Example – Matt Aug 07 '14 at 14:39

1 Answers1

1

I still don't see the exception that you do, even when using your Github project. Instead, the test fails at the VerifyAll line with this message:

Moq.MockVerificationException : The following setups were not matched:

IProductRepository mock => mock.GetProducts(x => x.Name == "Test" && x.IsActive)

However, I think you may be chasing a red herring, as Moq doesn't support setting up methods that take an Expression. See this answer.

Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88