0

I'm trying to do some Unit Test. I have the following controller:

using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private IProductRepository repository;
        public int PageSize = 4;
        //Declar the dependency on IProductRepository
        public ProductController(IProductRepository productRepository)
        {
            this.repository = productRepository;
        }
        // GET: Product

        public ViewResult List(int page = 1)
        {
            return View(repository.Products.OrderBy(p => p.ProductID).Skip((page-1) * PageSize).Take(PageSize));
        }
   
    }
}

I'm going to unit test the page pagination.

Here is my Unit test:

using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;
using SportsStore.WebUI.Controllers;
using System.Collections.Generic;
using System.Web.Mvc;

namespace SportsStore.UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }

        [TestMethod]
        public void Can_Paginate()
        {
            //Arrange
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
            {
                new Product {ProductID = 1, Name = "P1" },
                new Product {ProductID = 2, Name = "P2" },
                new Product {ProductID = 3, Name = "P3" },
                new Product {ProductID = 4, Name = "P4" },
                new Product {ProductID = 5, Name = "P5" }
            });

            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;

            //Act
            IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;

            //Assert
            Product[] prodArray = result.ToArray();
            Assert.IsTrue(prodArray.Length == 2);
            Assert.AreEqual(prodArray[0].Name, "P4");
            Assert.AreEqual(prodArray[1].Name, "P5");
        }
    }
}

I get the following error message in my test file:

enter image description here

What does this mean?

3
  • your project dll use newer dll than the dll you referenced in your test project. just remove the dll from your test project and than add the correct one. Commented Oct 19, 2015 at 11:42
  • @OldFox: How do I remove the dll? Commented Oct 19, 2015 at 11:43
  • I posted it as an answer... Commented Oct 19, 2015 at 11:55

1 Answer 1

1

Your project dll use newer dll than the dll you referenced in your test project. just remove the dll from your test project and than add the correct one:

Select the test project in Solution explorer -> References -> right click on System.Web.Mvc -> remove (How to: Add or Remove References)

Than add the correct version of System.Web.Mvc which is 5.2.3.0

Edit

The ASP.NET MVC has a nuget package. This is correct way to add the assembly

Sign up to request clarification or add additional context in comments.

4 Comments

Should I choose the Framework och Extension in the Assemblies tab?
@Bryan I don't know the exact location, but if you click on the assembly then you'll see the version on the right panel.
Yes, but how do I add a new version of the dll?
@Bryan the Dll which you are looking for has a nuget package. Right click on References -> Manage nuget packages -> in the search box type mvc and then add the package.(I edited my answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.