So, I'm using moq for testing, but I ran into a problem that prevents me from mocking correctly, at least I think so. This is my repository class:
public interface IAccountsRepository
{
IQueryable<Account> Accounts { get; }
IQueryable<Account> AccountsPaged(int pageSize, int selectedPage);
}
This is one of the implementations (fake):
public class FakeAccountsRepository : IAccountsRepository
{
private static readonly IQueryable<Account> FakeAccounts = new List<Account> {
new Account {RegistrationEmail = "[email protected]"},
new Account {RegistrationEmail = "[email protected]"},
new Account {RegistrationEmail = "[email protected]"},
new Account {RegistrationEmail = "[email protected]"},
new Account {RegistrationEmail = "[email protected]"}
}.AsQueryable();
public IQueryable<Account> Accounts
{
get { return FakeAccounts; }
}
public IQueryable<Account> AccountsPaged(int pageSize, int selectedPage)
{
return FakeAccounts.Skip((selectedPage - 1)*pageSize).Take(pageSize).AsQueryable();
}
}
This is a Controller definition that works perfectly with real page and fake or sql data (IoC) inside a real web page:
public class AccountsController : Controller
{
private IAccountsRepository _accountsRepository;
public int PageSize = 3;
public AccountsController(IAccountsRepository accountsRepository)
{
this._accountsRepository = accountsRepository;
}
public ViewResult List(int selectedPage)
{
return View(_accountsRepository.AccountsPaged(PageSize, selectedPage).ToList());
}
}
This is a moq method:
static IAccountsRepository MockAccountsRepository(params Account[] accs)
{
// Generate an implementor of IAccountsRepository at runtime using Moq
var mockProductsRepos = new Moq.Mock<IAccountsRepository>();
mockProductsRepos.Setup(x => x.Accounts).Returns(accs.AsQueryable());
return mockProductsRepos.Object;
}
it works fine with this implementation of List pagination:
public ViewResult List(int selectedPage)
{
return View(_accountsRepository.Accounts.Skip((selectedPage - 1) * PageSize).Take(PageSize).ToList());
}
but it fails when using this:
public ViewResult List(int selectedPage)
{
return View(_accountsRepository.AccountsPaged(PageSize, selectedPage).ToList());
}
Without changing test and changing only List implementation (doing pagination only on .Accounts) it all works, but when I try to use AccountsPaged method, it Fails returning no elements. In real usage, on a web page, it works both ways.
Please advise, thank you.
EDIT: If I do this:
mockProductsRepos.Setup(x => x.AccountsPaged(Moq.It.IsAny<int>(), Moq.It.IsAny<int>())).Returns(accs.AsQueryable());
I get 5 items returned instead of 2.