Mapper not initialized. Call Initialize with appropriate configuration.

System.InvalidOperationException:“Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.”

 

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!

share improve this question
 
   
What test framework are you using, MSTest? – LukeW Aug 17 '16 at 14:48
   
@LukeW: Yes, it is MSTest. – user1990 Aug 17 '16 at 14:50
1 
@user1990, Are you calling MappingConfig.RegisterMaps(); in your unit tests? – Nkosi Aug 17 '16 at 15:25 

1 Answer

up vote 3down voteaccepted

You need to create/register the mappings for your unit tests as well as the Application_Start() is not executed. It is associated with IIS, which is not running during unit tests. You have to manually call the mapping configurations.

[TestClass]
public class HomeControllerTests { [ClassInitialize] public static void Init(TestContext context) { MappingConfig.RegisterMaps(); } [TestMethod] public void ItemsListing() { HomeController controller = new HomeController(); ViewResult result = controller.ItemsListing() as ViewResult; Assert.IsNotNull(result); } }

In the above test the mapping configuration is done in a method decorated with [ClassInitialize]attribute which

ClassInitializeAttribute Class Identifies a method that contains code that must be used before any of the tests in the test class have run and to allocate resources to be used by the test class.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值