0

I am planning to write unit testcase for my MVC controller.

I am using NUnit Framework.

here is my Controller method:

public async Task<ActionResult> SearchView() { List<Role> allRoles = (await _nmClient.GetDataAsync<IEnumerable<Role>>(Session, "/UserSvc/v1/Roles?skip=0&take=" + Constants.MaxSearchRowNumber)).ToList(); model.Roles=_helper.GetAvailableRoles(Session.Contents["Session"], allRoles, true); List<LicenseType> allLicenseTypes = (await _Client.GetPlatformDataAsync<IEnumerable<LicenseType>>(Session, "/UserSvc/v1/Types?skip=0&take=" + Constants.MaxSearchRowNumber)).ToList(); model.TypesJson = Newtonsoft.Json.JsonConvert.SerializeObject(allLicenseTypes); return View("SearchUsers", model); } 

Here first I am trying to validate the view name,But I am facing issue with getting the view name from action result.

here is my Test Method:

[Test] public void TestSearchUserView()  {      string expected= "SearchUserView";      PlatformUserController controller = new PlatformUserController();      var result= controller.SearchUserView() as Task<ActionResult>;                   //Assert.AreEqual("SearchUserView", result.);   } 

Please let me know how can I mock the response of service as well.

2 Answers 2

1

I got the issue fixed, its because of different versions of System.Web.MVC reference used in Unit Test project and Web project.

Please make sure 'System.Web.MVC' dll referenced in both Unit Test project and Web Project should be same.

In my case, the version used in Web->v4.0.0.0 the version used in Unit Test Project->4.0.0.1

I installed the version 'v4.0.0.0' in Unit Test Project and it worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example of how to get the View name

[TestMethod] public async Task TestSearchUserView() { //Arrange string expected = "SearchUsers"; var controller = new PlatformUserController(); //Act var actionResult = await controller.SearchUserView(); //Assert Assert.IsNotNull(actionResult); var viewResult = actionResult as ViewResult; Assert.IsNotNull(viewResult); Assert.AreEqual(expected, viewResult.ViewName); } 

You will need to modify your controller to allow dependencies to be injected. with injectable interfaces, you can use a mocking framework like Moq to generate mocks for your controller.

From your example the following are good candidates for dependencies that can be injected into the controller

  • _nmsPlatformClient
  • _helper

5 Comments

sorry for delayed response,I tried with the above code and I am still getting null for viewResult. Can you please help me why is it happening
Are you getting any exceptions? I also noticed a typo when I copied your test example and made changes to give an answer. In you action you name the view SearchUsers but in test yo are expecting SearchUsersView. I'll edit to match what was in the original action.
no exceptions,I am getting when tried to debug on the controller side.Not sure why the ViewResult is null.ActionResult is coming as single line 'System.web.Mvc.ViewResult' .
Is actionResult in the test null as well?
the actionResult is not null,the response is 'System.Web.MVC.ViewResult' as single line instead of object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.