1

for unit-testing my asp.net MVC web application, I'd like to mock my IGenericRepository (I'm using Moq).

The method that has to be mocked looks like this:

IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, 
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");

I've already mocked the repository without any problems:

useraccountRepository = new Mock<IGenericRepository<Useraccount>>();

Now I'd like to tell Moq that when the Get method of my interface is called, a list of useraccounts should be returned:

useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email && u.Password == useraccount.Password)).Returns(useraccounts);

I think there is a mistake in my second lambda because this one works:

useraccountRepository.Setup(r => r.Get(null, null, "")).Returns(useraccounts);

But where's my error?

What also works:

useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email 
            && u.Password == useraccount.Password, null, "")).Returns(useraccounts);

It looks like the default parameter values don't apply in my mock. Why is that?

If I use

useraccountRepository.Setup(r => r.Get(u => u.Email == useraccount.Email 
            && u.Password == useraccount.Password, null, "")).Returns(useraccounts);

anyway, then the method in my controller throws an exception when the Get message is called:

private bool validateUser(Useraccount useraccount)
    {
        try
        {
            Useraccount useraccountLogin = UnitOfWork.UseraccountRepository.Get(
                u => u.Email == useraccount.Email && u.Password == useraccount.Password).Single<Useraccount>();

            return (useraccountLogin != null);
        }
        catch (Exception exc)
        {
            return false;
        }            
    }

Where are my mistakes?

Thank you in advance. Michael

mosquito87
  • 4,270
  • 11
  • 46
  • 77
  • **What** doesn't work and **which** exception is thrown? You navigate around specifics like a pro... – Daniel Hilgarth Feb 18 '13 at 17:58
  • Sorry for confusing you. If inside my unit test the validateUser(...)-method is called (from inside a controller method) it throws an exception where UseraccountRepository.Get(...) is called: [System.InvalidOperationException] = {"Sequence contains no elements"}. " at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)\r\n at ...". – mosquito87 Feb 18 '13 at 19:39

1 Answers1

0

According to this question Moq.Mock<T> - how to setup a method that takes an expression, setting constraints on expression parameters is unsupported by Moq.

Community
  • 1
  • 1
Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46
  • What I've found and at least works is: useraccountRepository.Setup(r => r.Get(It.IsAny>>(), null, "")).Returns(useraccounts); Is there a better way or is that already the best way for mocking the repository method I've shown? – mosquito87 Feb 18 '13 at 19:54
  • I think it is your best bet. But obviously you have a problem when you need to mock two different calls with distinct expressions. I would consider scrapping the generic repo though. Many experts consider it an antipattern, partly due to poor testability. – Jonas Høgh Feb 18 '13 at 20:34
  • Thank you for your help. Your link took me to the right track. :) – mosquito87 Feb 19 '13 at 07:14