Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3751690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:06:37+00:00 2026-05-19T09:06:37+00:00

I have the following controller action: public ActionResult Edit(int id) { var news =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:06:38+00:00Added an answer on May 19, 2026 at 9:06 am

    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:

    var model = actual
      .AssertViewRendered()
      .WithViewData<NewsEditModel>();
    
    // Check that model is not null
    Assert.That(model, Is.Not.Null);  
    
    // Check that model ID is same as passed into action method
    Assert.That(model.Id, Is.EqualTo(1));
    // or alternatively ...
    model.Id.ShouldBe(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following action method in my Tournament controller: public ActionResult Edit(int id)
I have the following controller Action: public ActionResult Details(int id) { Seguimiento seguimiento =
I have the following code: [HttpGet] public ActionResult Edit(int req) { var viewModel =
Lets say i have the following controller: // // GET: /Courses/Edit/5 public ActionResult Edit(int
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
Inside my controller's action I have the following code: public ActionResult GridAction(string id) {
I have something similar to the following method: public ActionResult Details(int id) { var
On ASP.NET MVC 3, assume that we have following controller action: public ActionResult Index()
I have the following controller action: public ActionResult Details(string pk) { IEnumerable<ContentDetail> model =
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.