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?
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);
Related
I am creating some Nunit tests for an MVC application. I am writing a test case for a methods in my Controller class. I am using Nsubstitute for Mocking the object.
I am learning about Nunit and Nsubstitue and I don't know, how to pass my Model value which was mocked in the testcase method to my controller method.
Below is my method in the controller class:
public ActionResult Manage(string id)
{
var clusterCollections = ReadXml();
int clusterIndex = clusterCollections.ClusterCollectionList.FindIndex(a => a.ClusterId == id);
var model = new ClusterManagementModel()
{
ClusterNodeDetailsList = BindClusterDetailsToGrid(id),
DropDownListClusterName = BindClusterNameToDropDown(),
CurrentClusterId = clusterIndex,
CurrentClusterName = id,
HStatus = Hstatus(id),
IStatus = Istatus(id)
};
return View(model);
}
This is the TestCase I have written:
[TestCase]
public void TestManage()
{
var ManagementController = Substitute.ForPartsOf<ClusterManagementController>();
var ManagementModel = Substitute.ForPartsOf<ClusterManagementModel>();
ClusterCollections clusterCollection = new ClusterCollections();
List<ClusterNodeDetails> ClusterNodes = new List<ClusterNodeDetails>();
List<DDL_ClusterName> DropDownListClusterName = new List<DDL_ClusterName>();
ManagementController.ReadXml().Returns(clusterCollection);
ManagementModel = new ClusterManagementModel()
{
ClusterNodeDetailsList = ClusterNodes,
DropDownListClusterName = DropDownListClusterName,
CurrentClusterId = 1,
CurrentClusterName = "UnitTesting",
HStatus = "True",
IStatus = "Success"
};
var result = ManagementController.Manage("1") as ActionResult;
Assert.AreEqual(ManagementModel, result);
}
If I have made mistakes in the TestCase Method please correct me.
If my TestCase is wrong, can you give me a suggestion how to write a TestCase for the above method (public ActionResult Manage(string id))
I don't know, how to pass Model value
The short answer is that in its current form, you can't pass the model you've created in your test to your controller. This is a common problem that people run into when they first start trying to unit test their code. The way out of the hole is to start writing your production code with testing in mind.
One approach that is common is to extract dependencies from your class and to inject these dependencies through the constructor for the class. So, you might extract your model creation logic into a ModelFactory and modify your controller to have a constructor like this:
public ManagementController(IModelFactory modelFactory) {
_modelFactory = modelFactory;
}
There are various libraries that can help to inject these dependencies (Ninject, AutoFac etc). But if you don't want to / can't use them, then you will also need to add a default constructor that sets up your dependencies to default concrete implementations:
public ManagementController() {
_modelFactory = new ModelFactory();
}
This allows you to create Stubbed / Mocked / Substituted implementations of IModelFactory and inject it into your controller / other class under test. So, your test might start off something like this:
List<ClusterNodeDetails> ClusterNodes = new List<ClusterNodeDetails>();
List<DDL_ClusterName> DropDownListClusterName = new List<DDL_ClusterName>();
var model = new ClusterManagementModel()
{
ClusterNodeDetailsList = ClusterNodes,
DropDownListClusterName = DropDownListClusterName,
CurrentClusterId = 1,
CurrentClusterName = "UnitTesting",
HStatus = "True",
IStatus = "Success"
};
var modelFactory = Substitute.For<IModelFactory>();
modelFactory.CreateClusterManagementModel( /* args for model creation */).Returns(model);
var sut = new ManagementController(modelFactory);
var result = sut.Manage("1") as ActionResult;
You also need to think about what it is you're trying to test. By extracting the dependencies you're able to focus on the logic in your controller and focus your tests on that logic. It's very easy when you start using Mocks to get into a situation where you're not actually testing anything at all, other than whether or not you've set your Mocks up correctly. Remember, they are there to help you reproduce specific scenarios you need to force your production code to follow a particular flow, not to replace the logic contained in your production code.
I'm building a website in MVC 4 & using Automapper to map from domain objects to Viewmodel objects. I have injected Automapper as stated here http://rical.blogspot.in/2012/06/mocking-automapper-in-unit-testing.html
and it's working fine inside action methods while debugging, but during unit testing the action method when I inject automapper service I find that service.map is returning null. But while debugging the mapping is fine. I'm not being able to find the reason, trying for over 4 hrs. I have a domain class called Interview & its corrosponding viewmodel as InterviewModel. I have initialized mapping as CreateMap(); in automapper profile config, that has been called from global startup method. Below is the controller & action...
public class NewsAndViewsController : Controller
{
private IInterviewRepository repository;
private IMappingService mappingService;
public NewsAndViewsController(IInterviewRepository productRepository, IMappingService autoMapperMappingService)
{
repository = productRepository;
mappingService = autoMapperMappingService;
}
[HttpPost, ValidateAntiForgeryToken]
[UserId]
public ActionResult Edit(InterviewModel interView, string userId)
{
if (ModelState.IsValid)
{
var interView1 = mappingService.Map<InterviewModel, Interview>(interView);
**// THE ABOVE LINE RETURNING NULL WHILE RUNNING THE BELOW TEST, BUT NOT DURING DEBUGGING**
repository.SaveInterview(interView1);
TempData["message"] = string.Format("{0} has been saved", interView.Interviewee);
return RedirectToAction("Create");
}
return View(interView);
}
}
[TestMethod]
public void AddInterview()
{
// Arrange
var interviewRepository = new Mock<IInterviewRepository>();
var mappingService = new Mock<IMappingService>();
var im = new InterviewModel { Interviewee="sanjay", Interviewer="sanjay", Content="abc" };
mappingService.Setup(m => m.Map<Interview, InterviewModel>(It.IsAny<Interview>())).Returns(im);
var controller = new NewsAndViewsController(interviewRepository.Object, mappingService.Object);
// Act
var result = controller.Edit(im, "2") as ViewResult;
// Assert - check the method result type
Assert.IsNotInstanceOfType(result, typeof(ViewResult));
}
In your test you've got your Interview and InterviewModel classes crossed up in the mappingService.Setup() call (as an aside, I think you could use better naming conventions, or don't use var, to keep your objects clear - "im", "interview" and "interview1" don't make it easy to follow which is the model and which is the view object).
Try this:
[TestMethod]
public void AddInterview()
{
// Arrange
var interviewRepository = new Mock<IInterviewRepository>();
var mappingService = new Mock<IMappingService>();
var interview = new Interview();
var im = new InterviewModel { Interviewee="sanjay", Interviewer="sanjay", Content="abc" };
mappingService.Setup(m => m.Map<InterviewModel, Interview>(im).Returns(interview);
var controller = new NewsAndViewsController(interviewRepository.Object, mappingService.Object);
// Act
var result = controller.Edit(im, "2") as ViewResult;
// Assert - check the method result type
Assert.IsNotInstanceOfType(result, typeof(ViewResult));
}
I am new to nSubstitute. And I am writing test method for my controller class. I have a TestMethod called GetDefaultStateTest() which having Substitute class as shown below
[TestMethod]
public void GetDefaultStateTest()
{
var _GetDefaultState = Substitute.For<CustomerController>(ICustomer cus);
Assert.IsNotNull(_GetDefaultState.GetDefaultState());
}
Because my controller class having parameterized constructor as below.
public class CustomerController : Controller
{
private readonly ICustomer _customer;
public CustomerController(ICustomer customer)
{
_customer = customer;
}
public string GetDefaultState()
{
// Get default state from settings table
List<tblSettings> settings = new List<tblSettings>();
// Calling service method GetSettings
settings = _customer.GetSettings();
var defaultState = from setting in settings
where setting.Desc == "DefaultState"
select setting.Settings;
string strState = "";
foreach (var oState in defaultState)
{
strState = oState;
break;
}
return strState;
}
}
While run the test method, it raise null reference issue. Because of parameter ICustomer is null
var _GetDefaultState = Substitute.For<CustomerController>(ICustomer cus);
How to resolve this problem.
If you are testing your controller class then you do not want to substitute for it, you want to use a real one (otherwise you'd just be testing a fake object "works" :)). Where you may want to substitute is for that class's dependencies, in this case, ICustomer.
[TestMethod]
public void GetDefaultStateTest()
{
var customer = Substitute.For<ICustomer>();
var controller = new CustomerController(customer);
Assert.IsNotNull(controller.GetDefaultState());
}
You may then want to fake out the ICustomer.GetSettings() method so you can test what your controller does with that data:
[TestMethod]
public void GetDefaultStateTestFromSettings()
{
var customer = Substitute.For<ICustomer>();
customer.GetSettings().Returns(somethingSensible);
var controller = new CustomerController(customer);
Assert.AreEqual(expectedDefaultState, controller.GetDefaultState());
}
As an aside, sometimes it makes more sense to use real objects (say, a real implementation of ICustomer) rather than substitutes. This will depend on how well-defined the interactions with the dependencies are, where you want to define the boundaries of your system under test, and how much confidence the test gives you that the system under test is working correctly. Or put more simply, whatever makes it easy and reliable to test. :)
Hope this helps.
I am writing an app that I have been deploying to appharbor. I am having trouble getting my project to build now because I have expanded my tests. I believe the issue is that I am using a db initializer to populate the database with test seed data. These tests pass on my local box but once I deploy the tests fail on appharbor. I suspect I need to mock data but I am not sure how to do this. As an example, here is a controller test that I have for one of my action methods.
Controller
// GET: /Lead/Url
// TODO: Add optional url parameters
public ActionResult Url(string pfirstname, string plastname, string phone, int leadsource)
{
var lead = new Lead();
//store
lead.parent_FirstName = pfirstname;
lead.parent_LastName = plastname;
lead.parent_Phone = phone;
lead.LeadSourceID = leadsource;
lead.AgentID = 1;
if (ModelState.IsValid)
{
leadRepository.InsertLead(lead);
leadRepository.Save();
ViewBag.Message = "Success";
}
return View(lead);
}
//
// POST: /Lead/URL
[HttpPost, ActionName("Url")]
public ActionResult Url(Lead lead)
{
return View();
}
Unit Test
[TestMethod]
public void LeadUrl()
{
//ARRANGE
ILeadRepository leadrepository = new LeadRepository(new LeadManagerContext());
Database.SetInitializer<LeadManagerContext>(new LeadManagerInitializer());
LeadController controller = new LeadController(leadrepository);
//ACT
ViewResult result = controller.Url("Brad", "woods","465-456-4965",1) as ViewResult;
var lead = (Lead)result.ViewData.Model;
//ASSERT
Assert.AreEqual("Success" ,result.ViewBag.Message);
/*check for valid data */
Assert.AreEqual("Brad", lead.parent_FirstName);
}
Could someone please explain what I need to do next in order to improve code like this and get it to run again on app harbor successfully?
Actually you haven't verified interactions between controller and it's dependencies (repository). And this is the most important part - controller should pass your Lead object to repository. And then call Save (consider also to Unit Of Work pattern).
Also you should test controller in isolation, only this way you could be sure, that failing controller's test is an issue of controller, not of LeadRepository or LeadManagerInitializer.
// Arrange
Lead expected = CreateBrad();
var repository = new Mock<ILeadRepository>();
LeadController controller = new LeadController(repository.Object);
// Act
ViewResult result = (ViewResult)controller.Url("Brad", "woods", "465-456", 1);
// Assert
Lead actual = (Lead)result.ViewData.Model;
// All fields should be equal, not only name
Assert.That(actual, Is.EqualTo(expected));
Assert.AreEqual("Success", result.ViewBag.Message);
// You need to be sure, that expected lead object passed to repository
repository.Verify(r => r.InsertLead(expected));
repository.Verify(r => r.Save());
BTW I'd moved expected Lead creation to separate method:
private Lead CreateBrad()
{
Lead lead = new Lead();
lead.parent_FirstName = "Brad";
lead.parent_LastName = "woods";
lead.parent_Phone = "465-456";
lead.LeadSourceID = 1;
lead.AgentID = 1;
return lead;
}
Also you should override Equals method for Lead instances comparison:
public class Lead
{
// your current code here
public override bool Equals(object obj)
{
Lead other = obj as Lead;
if (other == null)
return false;
return other.parent_FirstName == parent_FirstName &&
other.parent_LastName == parent_LastName &&
// compare other properties here
other.AgentID == AgentID;
}
// also override GetHashCode method
}
BTW Why you don't pass Lead object to your action method (via POST message)?
You have to stub your repository. The easiest way to do that is to use mocking framework (I prefer Moq), and stub each method.
Something like this (for Moq):
var repository = new Mock<ILeadReporisory>();
repository.Setup(r => r.InsertLead(It.IsAny<Lead>()));
//raise, rinse, repeat
LeadController controller = new LeadController(repository.Object);
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));
}