0

I am trying to test my controller and quite new to this testing. Using NUnit and Moq I keep getting a null result. Here is my test, am I missing a step? like I mentioned it's my first project.

[TestFixture]
class CustomerServiceTests
{
    public Mock<IRepository<Customer>> CustomerRepository = new Mock<IRepository<Customer>>(); 

    public Customer Customer;

    [SetUp]
    public void Setup()
    {
        Customer = new Customer()
        {
            Id = 1 << Can I set the ID?
            Address = "3 Lakeview Terrace",
            City = "New York",
            Email = "[email protected]",
            FirstName = "Joe",
            LastName = "Dirt",
            Phone = "888-888-8888",
            Province = "NY"
        };         
    }


    [Test]
    public void CanCreateCustomer()
    {
        // ARRANGE
        var controller = new CustomerController(CustomerRepository.Object);
        controller.Create(Customer);

        // ACT
        var customer = CustomerRepository.Setup(c => c.Find(1)).Returns(new Customer());

        // ASSERT
        Assert.AreEqual(Customer, customer);
    }      
}

CONTROLLER

// POST: /Customer/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Customer customer)
    {
        if (ModelState.IsValid)
        {
            _customerRepository.Add(customer);
            return RedirectToAction("Index");
        }
        return View(customer);
    }

IREPOSITORY

public interface IRepository<T>  where T : class 
    {
        IQueryable<T> Get { get; }
        T Find(object[] keyValues);
        T Find(int id);
        void Add(T entity);
        void Update(T entity);
        void AddOrUpdate(T entity);
        void Remove(object[] keyValues);
        void Remove(T entity);

    }

2 Answers 2

2

Your test method should look like this:

[Test]
public void CanCreateCustomer()
{
    // ACT
    var controller = new CustomerController(CustomerRepository.Object);
    controller.Create(Customer);

    // VERIFY
    CustomerRepository.Verify(c => c.Add(It.Is.Any<Customer>(),Times.Once()));
}

To add an error to the modelstate you can do it like this:

controller .ModelState.AddModelError("key", "error message");

Otherwise the modelState is valid.

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

4 Comments

Thank you! Just need to figure out how you Assert the view. :)
What do you want to test then? The content in the view or which view you are returning?
both eventually, but right now just what view is being returned.
Created a new post for test the returning view, to test the content is harder and not so usual. Maybe you want to know what kind of type you returned and it's data.
0

To test the viewName you need to change your code a little bit.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Customer customer)
{
    if (ModelState.IsValid)
    {
        _customerRepository.Add(customer);
        return RedirectToAction("Index");
    }
    return View("Create", customer);
}

Test:

[Test]
public void ReturnView()
{
   // ACT
   var controller = new CustomerController(CustomerRepository.Object);
   var result = controller.Create(Customer);

   // ASSERT
   Assert.AreEqual("Create", ((ViewResult)result).ViewName);
}

To test the returned data:

[TestMethod]
public void TestMethod2()
{
    var controller = new CustomerController();
    var result = controller.Create(Customer);

    Assert.AreEqual(1, ((Asd)((ViewResult)result).ViewData.Model).Id);
 }

Comments

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.