Here is the code for the method that I‘m developing the unit test for:
public ActionResult ItemsListing()
{
var itemsList = itemsRepository.GetItems(true);
if (itemsList.Count() > 0)
{
var itemsListVMs = Mapper.Map<IEnumerable<Item>, IEnumerable<itemsListingViewModel>>(itemsList);
return View(itemsListVMs);
}
else
{
return RedirectToAction("Home");
}
}
Following is the code from the mapping configuration file:
public static class MappingConfig
{
public static void RegisterMaps()
{
Mapper.Initialize(config =>
{
config.CreateMap<Item, itemsListingViewModel>();
});
}
}
And I have initialized mapper in the Application_Start()
event of the Global.asax
as below:
MappingConfig.RegisterMaps();
Below is the simple test method that I‘m trying to run:
[TestMethod]
public void ItemsListing()
{
HomeController controller = new HomeController();
ViewResult result = controller.ItemsListing() as ViewResult;
Assert.IsNotNull(result);
}
It works fine when I simply run the application. But when I try to run the unit test method, it shows the mentioned error message. Can anyone help me to get over this issue? Thanks!
MappingConfig.RegisterMaps();
in your unit tests? – Nkosi Aug 17 ‘16 at 15:25