使用视图模型测试 get 操作
我有以下控制器操作:
public ActionResult Edit(int id)
{
var news = newsRepository.GetNewsByID(id);
Mapper.CreateMap<News, NewsEditModel>();
var newsEditModel =
(NewsEditModel)Mapper.Map(news, typeof(News), typeof(NewsEditModel));
return View(newsEditModel);
}
以及相应的测试:
[Test]
public void Edit_should_render_view()
{
// Arrange
var id = 1;
var newsEditModel = new NewsEditModel();
// Act
var actual = sut.Edit(id);
// Assert
actual
.AssertViewRendered()
.WithViewData<NewsEditModel>()
.ShouldBe(newsEditModel);
}
在 NUnit GUI 中,我收到以下错误:
MyProject.Web.UnitTests.Controllers.NewsControllerTests.Edit_should_render_view: MvcContrib.TestHelper.AssertionException :是 MyProject.Web.Common.ViewData.NewsEditModel 但预期是 MyProject.Web.Common.ViewData.NewsEditModel
我不知道如何编写相应的单元测试。有人可以帮我吗?
I have the following controller action:
public ActionResult Edit(int id)
{
var news = newsRepository.GetNewsByID(id);
Mapper.CreateMap<News, NewsEditModel>();
var newsEditModel =
(NewsEditModel)Mapper.Map(news, typeof(News), typeof(NewsEditModel));
return View(newsEditModel);
}
And the corresponding test:
[Test]
public void Edit_should_render_view()
{
// Arrange
var id = 1;
var newsEditModel = new NewsEditModel();
// Act
var actual = sut.Edit(id);
// Assert
actual
.AssertViewRendered()
.WithViewData<NewsEditModel>()
.ShouldBe(newsEditModel);
}
In the NUnit GUI I get the following error:
MyProject.Web.UnitTests.Controllers.NewsControllerTests.Edit_should_render_view:
MvcContrib.TestHelper.AssertionException : was MyProject.Web.Common.ViewData.NewsEditModel but expected MyProject.Web.Common.ViewData.NewsEditModel
I don't know how to write the corresponding unit test. Can someone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的测试正在比较
NewsEditModel
的两个不同实例 - 一个是您在测试代码中创建的实例,另一个是在操作方法中创建的实例。如果您想在测试中检查模型属性值,您可以这样做:
Your test is comparing two different instances of
NewsEditModel
- one instance that you create in your test code, and the other that is created in the action method.If you want to examine the model property values as part of your test, you could do this: