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?