Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 2 - Please tell me why repository is returning no data in the controller in unit test
    text
    copied!<p>The problem I'm encountering is that I have a unit test failing and as I walk through the logic I can't figure out why. I have a unit test that fails when viewing products from a single category (i.e. products that are of the category "Books").</p> <p>As I debug through my test I notice that zero "products" are returned in the first couple lines of the StoreController.List method. This occurs if either GetProducts() or GetProductsByCategoryTypeDescription() is called, nothing is returned. According to my mocks, data should be returning and I'm confused on why it is not. If needed I can also show my entities, but I didn't think they'd matter in this case...</p> <p>This biggest thing to note is that my test fails, but when manually testing the application it appears to work fine. I believe the issue is with how I'm setting up my mocks potentially.</p> <ul> <li>.NET: V 4.0 </li> <li>Testing tool: NUnit v2.5 </li> <li>Mock Framework: Moq 4.0</li> <li>DI: Ninject 2.2</li> <li>ORM: Linq 2 Sql</li> </ul> <p>My Repositories:</p> <pre><code>public class ProductCategoryRepository : IProductCategoryRepository { private Table&lt;ProductCategory&gt; productCategoryTable; private IQueryable&lt;ProductCategory&gt; ProductsCategories { get { return productCategoryTable; } } public ProductCategoryRepository(string connectionString) { productCategoryTable = (new DataContext(connectionString)).GetTable&lt;ProductCategory&gt;(); } public IQueryable&lt;ProductCategory&gt; GetProductCategories() { return ProductsCategories; } } </code></pre> <p>}</p> <pre><code>public class ProductRepository : IProductRepository { private Table&lt;Product&gt; productTable; private IQueryable&lt;Product&gt; Products { get { return productTable; } } public ProductRepository(string connectionString) { var dc = new DataContext(connectionString); productTable = dc.GetTable&lt;Product&gt;(); } public IQueryable&lt;Product&gt; GetProducts() { return this.Products; } public IQueryable&lt;Product&gt; GetProductsByCategoryTypeDescription(string productCategoryDescription) { return this.Products.Where(x =&gt; x.ProductCategory.Description == productCategoryDescription); } } </code></pre> <p>My Controller that I'm having the problem with:</p> <pre><code> public ViewResult List(string category, int page = 1) //Use default value { var productsToShow = (category == null) ? productRepository.GetProducts() : productRepository.GetProductsByCategoryTypeDescription(category); var viewModel = new ProductListViewModel { Products = productsToShow.Skip((page - 1) * this.PageSize) .Take(PageSize) .ToList(), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = productsToShow.Count() }, CurrentCategory = category }; return View(viewModel); } </code></pre> <p>My Unit Test:</p> <pre><code> [Test] public void Can_View_Products_From_A_Single_Category() { // Arrange: If two products are in two different categories... IProductRepository productRepository = UnitTestHelpers.MockProductRepository( new Sermon { Name = "Sermon1", ProductCategory = new ProductCategory { ProductCategoryId = 1, Description = "Sermons" } }, new Sermon { Name = "Sermon2", ProductCategory = new ProductCategory { ProductCategoryId = 2, Description = "Books" } } ); IProductCategoryRepository productCategoryRepository = UnitTestHelpers.MockProductCategoryRepository( new ProductCategory { ProductCategoryId = 1, Description = "Sermons" }); var controller = new StoreController(productRepository, productCategoryRepository); // Act: ... then when we ask for one specific category var result = controller.List("Sermons", 1); // Arrange: ... we get only the product from that category var viewModel = (ProductListViewModel)result.ViewData.Model; viewModel.Products.Count.ShouldEqual(1); viewModel.Products[0].Name.ShouldEqual("Sermon1"); viewModel.CurrentCategory.ShouldEqual("Sermons"); } public static void ShouldEqual&lt;T&gt;(this T actualValue, T expectedValue) { Assert.AreEqual(expectedValue, actualValue); } public static IProductRepository MockProductRepository(params Product[] products) { // Generate an implementer of IProductRepository at runtime using Moq var mockProductRepos = new Mock&lt;IProductRepository&gt;(); mockProductRepos.Setup(x =&gt; x.GetProducts()).Returns(products.AsQueryable()); return mockProductRepos.Object; } public static IProductCategoryRepository MockProductCategoryRepository(params ProductCategory[] productCategories) { // Generate an implementer of IProductRepository at runtime using Moq var mockProductCategoryRepos = new Mock&lt;IProductCategoryRepository&gt;(); mockProductCategoryRepos.Setup(x =&gt; x.GetProductCategories()).Returns(productCategories.AsQueryable()); return mockProductCategoryRepos.Object; } public static void ShouldBeRedirectionTo(this ActionResult actionResult, object expectedRouteValues) { var actualValues = ((RedirectToRouteResult)actionResult).RouteValues; var expectedValues = new RouteValueDictionary(expectedRouteValues); foreach (string key in expectedValues.Keys) actualValues[key].ShouldEqual(expectedValues[key]); } </code></pre> <p>-</p> <pre><code>public static class UnitTestHelpers { public static void ShouldEqual&lt;T&gt;(this T actualValue, T expectedValue) { Assert.AreEqual(expectedValue, actualValue); } public static IProductRepository MockProductRepository(params Product[] products) { // Generate an implementer of IProductRepository at runtime using Moq var mockProductRepos = new Mock&lt;IProductRepository&gt;(); mockProductRepos.Setup(x =&gt; x.GetProducts()).Returns(products.AsQueryable()); return mockProductRepos.Object; } public static IProductCategoryRepository MockProductCategoryRepository(params ProductCategory[] productCategories) { // Generate an implementer of IProductRepository at runtime using Moq var mockProductCategoryRepos = new Mock&lt;IProductCategoryRepository&gt;(); mockProductCategoryRepos.Setup(x =&gt; x.GetProductCategories()).Returns(productCategories.AsQueryable()); return mockProductCategoryRepos.Object; } } </code></pre>
 

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