JJ.
JJ.

Reputation: 1

How to Implement and Test ASP.NET MVC FakeRepository

Would welcome any help.

I am learing to write code using ASP.NET MVC framework, I sort of sold on this concept.

My main stumbling block right now is how to setup and test a repository that replaces the database. To test out the MVC application, I have created a class and called it a fakerepository.cs This class implements methods from the IContactManagerRepository interface.

namespace MyTestMVCProject.Models
{
    public class FakeContactManagerRepository : IContactManagerRepository
    {
        IList<Contact> _contacts = new List<Contact>();

        #region IContactManagerRepository Members

        public Contact Create(Contact contact)
        {
            _contacts.Add(contact);
            return contact;
        }

        public Contact Edit(Contact contact)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public IList<Contact> ListContacts()
        {
            return _contacts;
        }

        #endregion
    }
}

In the attempted test below, I want to ensure that the Contact has been created and the ID value is correct.

[Test]
public void Test_02_ContactController_Passes_ViewData_To_Details_View()
{
    // Arrange
    ContactController _controller = new ContactController();

    // Act
    var _contact = new Contact
    {
        Id = 1,
        FirstName = "Donald",
        LastName = "Duck"
    };

    var _result = _controller.Create(_contact) as ViewResult;
    var contact = _result.ViewData.Model as Contact;

    // Assert
    Assert.AreEqual(1, _contact.Id);
}

Unfortunately the test always fails.

Of course I am very new to testing but I have picked up a lot in a short space of time by searching on google and watching ASP.NET MVC videos.

Can anybody suggest how I can test a fakerepository that returns a list to the ViewResult?

Upvotes: 0

Views: 102

Answers (1)

LukLed
LukLed

Reputation: 31842

Test could look like:

[Test]
public void PostingValidContactCreatesOneInRepositoryAndReturnsViewResult()
{
    // Arrange
    var controller = new ContactController(new FakeContactManagerRepository());

    // Act
    var contact = new Contact
    {
        Id = 1,
        FirstName = "Donald",
        LastName = "Duck"
    };

    var result = controller.Create(contact);

    //Assert there is one created Contact in repository
    Assert.AreEqual(1, Repository.ListContacts().Count());
    //Check if result is ViewResult
    Assert.IsInstanceOfType(result,typeof(ViewResult));
    //Assert item Id is 1
    Assert.AreEqual(1, Repository.ListContacts().First().ID);
    //Check if posting valid contact doesn't invalidate model state
    Assert.IsTrue(controller.ModelState.IsValid);
}

ContactController has to take IContactManagerRepository in constructor

public ContactController(IContactManagerRepository repository);

In tests you provide it with FakeContactManagerRepository, in real use you can inject your real repository.

EDIT

Your errors are:

var contact = _result.ViewData.Model as Contact;

Posted contact is not in _result.ViewData.Model, but in parameter of Create function.

var _result = _controller.Create(_contact) as ViewResult;

This is not error, but you can define like

var _result = _controller.Create(_contact)

and check if result is of correct type.

Assert.AreEqual(1, _contact.Id);

That makes no sense, you assigned _contact.Id few lines earlier. You should take contact out of repository and check.

Upvotes: 2

Related Questions