Вопрос

I am using ASP.NET MVC 3, MVCContrib, NUnit and Rhino Mocks. I have posted this but could not get an answer. People are focusing more on my coding that helping me get a decent answer to get this test to pass.

I am trying to test my Edit view. I'm not sure how to code the test for the view. It is a strongly typed view of type NewsEditViewData.

When the view loads, it gets a news item's data by ID from the news service. So in my test I created a stub of the news service:

var id = 1;
var news = new News();
newsServiceStub
   .Stub(x => x.FindById(id))
   .Return(news);

Now I need to map this news item to NewsEditViewData. I have a mapper (AutoMapper) that does this for me, and in my test I did the following:

newsMapperStub
   .Stub(x => x.Map(news, typeof(News), typeof(NewsEditViewData)))
   .Return(newsEditViewData);

I'm not sure if I am on the right track so far?

Then I have the following piece of code:

// Act
var actual = sut.Edit(id);

Don't work from my code, I want an answer following best practices. So what all do I need to check for in my assert part? Do I need to also check that a record was returned? I was thinking in the lines of something like:

actual
   .AssertViewRendered()
   .WithViewData<NewsEditViewData>()
   .ShouldBe(newsEditViewData);

This fails. Can someone please help me rewrite this test so that it passes. I want it to check all that needs to be checked.

Here is the full test:

[Test]
public void Edit_should_render_Edit_view()
{
   // Arrange
   var id = 1;
   var news = new News();
   var newsEditViewData = new NewsEditViewData();

   newsServiceStub
      .Stub(x => x.FindById(id))
      .Return(news);

   newsMapperStub
      .Stub(x => x.Map(news, typeof(News), typeof(NewsEditViewData)))
      .Return(newsEditViewData);

   // Act
   var actual = sut.Edit(id);

   // Assert
   actual
      .AssertViewRendered()
      .WithViewData<NewsEditViewData>()
      .ShouldBe(newsEditViewData);
}

UPDATE 2011-02-14:

In my NewsController I have the following:

private INewsService newsService;
private IMapper newsMapper;

public NewsController(INewsService newsService)
{
   Check.Argument.IsNotNull(newsService, "newsService");

   this.newsService = newsService;
   newsMapper = new NewsMapper();  // TODO: Use dependency injection
}

The action method looks like this:

public ActionResult Edit(int id)
{
   Check.Argument.IsNotZeroOrNegative(id, "id");

   var news = newsService.FindById(id);
   var newsEditViewData = (NewsEditViewData)newsMapper.Map(news, typeof(News), typeof(NewsEditViewData));

   return View(newsEditViewData);
}

The error that I am getting in NUnit is:

MyProject.Web.UnitTests.Controllers.NewsControllerTests.Edit_RenderView_EditView: MvcContrib.TestHelper.AssertionException : was MyProject.Web.Common.ViewData.NewsEditViewData but expected MyProject.Web.Common.ViewData.NewsEditViewData

Это было полезно?

Решение

You haven't shown none of your controller, repository, models. It's a question that is close to impossible to answer without this information. So lets start guessing. You have a model and a view model:

public class News { }
public class NewsEditViewData { }

I am leaving them without any properties for the purpose of this post. Then you probably have a service which is responsible for retrieving and saving your model (the view model should never appear as in/out argument of your service layer). The service should never know about the view model:

public interface INewsService
{
    News FindById(int id);
    void CreateNews(News news);
}

Then you probably have a mapper:

public interface IMapper
{
    object Map(object source, Type sourceType, Type destinationType);
}

And finally I suppose that you have a controller that you are trying to test:

public class NewsController : Controller
{
    private readonly INewsService _newsService;
    private readonly IMapper _newsMapper;

    public NewsController(INewsService newsService, IMapper newsMapper)
    {
        _newsService = newsService;
        _newsMapper = newsMapper;
    }

    public ActionResult Edit(int id)
    {
        // WARNING: Meaningless action ahead as it retrieves some
        // model from the service and passes this model to
        // the service back again for update. In the meantime
        // the model is converted to a view model using a mapper
        // and passed to the view. So totally meaningless in a real 
        // application but let's consider for the purpose of this demonstration
        var news = _newsService.FindById(id);
        _newsService.CreateNews(news);
        var newsEditViewData = (NewsEditViewData)_newsMapper.Map(news, typeof(News), typeof(NewsEditViewData));
        return View(newsEditViewData);
    }
}

OK, up until here it is you that should have provided this information.

And now I can start answering your question about the unit test which might look like this:

[Test]
public void Edit_should_fetch_news_model_from_service_given_an_id_parameter_create_news_and_pass_a_viewmodel_to_the_view()
{
    // arrange
    // TODO : move this part in the initialization section 
    // of your unit test to avoid repeating it on each method
    var newsServiceStub = MockRepository.GenerateStub<INewsService>();
    var newsMapperStub = MockRepository.GenerateStub<IMapper>();
    var sut = new NewsController(newsServiceStub, newsMapperStub);
    new TestControllerBuilder().InitializeController(sut);

    var news = new News();
    var id = 123;
    var newsEditViewData = new NewsEditViewData();
    newsServiceStub
        .Stub(x => x.FindById(id))
        .Return(news);

    newsMapperStub
        .Stub(x => x.Map(news, typeof(News), typeof(NewsEditViewData)))
        .Return(newsEditViewData);

    // act
    var actual = sut.Edit(id);

    // assert
    actual
        .AssertViewRendered()
        .WithViewData<NewsEditViewData>()
        .ShouldBe(newsEditViewData);
    newsServiceStub.AssertWasCalled(x => x.CreateNews(news));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top