Note that there are some explanatory texts on larger screens.

plurals
  1. POMOQ problem - mocked class returns incorrect data
    primarykey
    data
    text
    <p>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:</p> <pre><code>public interface IAccountsRepository { IQueryable&lt;Account&gt; Accounts { get; } IQueryable&lt;Account&gt; AccountsPaged(int pageSize, int selectedPage); } </code></pre> <p>This is one of the implementations (fake):</p> <pre><code>public class FakeAccountsRepository : IAccountsRepository { private static readonly IQueryable&lt;Account&gt; FakeAccounts = new List&lt;Account&gt; { new Account {RegistrationEmail = "first@demo.org"}, new Account {RegistrationEmail = "second@demo.org"}, new Account {RegistrationEmail = "third@demo.org"}, new Account {RegistrationEmail = "fourth@demo.org"}, new Account {RegistrationEmail = "fifth@demo.org"} }.AsQueryable(); public IQueryable&lt;Account&gt; Accounts { get { return FakeAccounts; } } public IQueryable&lt;Account&gt; AccountsPaged(int pageSize, int selectedPage) { return FakeAccounts.Skip((selectedPage - 1)*pageSize).Take(pageSize).AsQueryable(); } } </code></pre> <p>This is a Controller definition that works perfectly with real page and fake or sql data (IoC) inside a real web page:</p> <pre><code>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()); } } </code></pre> <p>This is a moq method:</p> <pre><code>static IAccountsRepository MockAccountsRepository(params Account[] accs) { // Generate an implementor of IAccountsRepository at runtime using Moq var mockProductsRepos = new Moq.Mock&lt;IAccountsRepository&gt;(); mockProductsRepos.Setup(x =&gt; x.Accounts).Returns(accs.AsQueryable()); return mockProductsRepos.Object; } </code></pre> <p>it works fine with this implementation of List pagination:</p> <pre><code>public ViewResult List(int selectedPage) { return View(_accountsRepository.Accounts.Skip((selectedPage - 1) * PageSize).Take(PageSize).ToList()); } </code></pre> <p>but it fails when using this:</p> <pre><code>public ViewResult List(int selectedPage) { return View(_accountsRepository.AccountsPaged(PageSize, selectedPage).ToList()); } </code></pre> <p>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.</p> <p>Please advise, thank you.</p> <p>EDIT: If I do this:</p> <pre><code>mockProductsRepos.Setup(x =&gt; x.AccountsPaged(Moq.It.IsAny&lt;int&gt;(), Moq.It.IsAny&lt;int&gt;())).Returns(accs.AsQueryable()); </code></pre> <p>I get 5 items returned instead of 2.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload