Mike Diaz
Mike Diaz

Reputation: 2065

TDD and MVC 3, Testing Models

I am trying to learn how to unit test and work with MVC 3 and I am getting stuck on the problem of How do I test two models. Here is the code

public class HomeController : Controller
{
    private IRepository _repository;

    public  HomeController(IRepository repository)
    {
        _repository = repository;
    }

    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View(_repository.GetAllGenres());
    }    
}

public interface IRepository
{
    IEnumerable<Genre> GetAllGenres();
    IEnumerable<Album> GetTopAlbums(int count);
}

and assume this is my Unit Testing

[TestFixture]
class HomeControllerTests
{
    [Test]
    public  void Test1()
    {                
        //Arrange
        var controller = new HomeController(new InMemoryRepository());
        var result = (ViewResult) controller.Index();

        Assert.AreEqual(10,((IEnumerable<Genre>)result.ViewData.Model).Count());
    }

    [Test]
    public  void Test2()
    {
        var controller = new HomeController(new InMemoryRepository());
        var result = (ViewResult) controller.Index();
        //I Want to be able to do something like this
        Assert.AreEqual(5,result.ViewData.Model.GetTopAlbums(5).Count);
    }
}

Now my question is How exactly do I go about making something like I want work. Or do I create a ChildActionOnly Method that is responsible for returning the Top Albums.

Upvotes: 2

Views: 1290

Answers (2)

tugberk
tugberk

Reputation: 58444

I have a very good solution for you. See the below two blog posts:

Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle

How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking

Basically, it is what @Jesse already suggested but those two posts also has some other features there which might be helpful for you.

Upvotes: 0

Jesse
Jesse

Reputation: 8393

Have you considered using a mocking framework to assist with you tests? For example you could make use of mock on your GetTopAlbums call. My preference is moq but there are several great mocking frameworks to choose from.

Note, this is a pretty simple example but you could easily create a test helper to generate a list with your expected number of albums:

[Test]
public void Index_Get_Should_Lookup_Top_Albums_And_Return_Index_View()
{
    // arrange
    var expectModel = new List<Album>
                            {
                                new Album{Artist= "joe", Tracks = 16},
                                new Album{Artist= "doe", Tracks = 23},
                            };

    _repository.Setup(x => x.GetTopContacts(It.IsAny<int>())).Returns(expectModel);

    var controller = new HomeController(_repository.Object);

    // act
    var result = controller.TopContacts();
    var model = result.ViewData.Model as IEnumerable<Album>;

    // assert
    Assert.AreEqual(2, model.Count());
}

Upvotes: 2

Related Questions