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!

asked Aug 17 '16 at 14:42
user1990

6010
 
    
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 

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.

最新文章

  1. 应用程序框架实战二十九:Util Demo介绍
  2. if语句,case语句
  3. mysql5.5的安装与配置(亲测版)
  4. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点
  5. [docker] 管理docker容器中的数据
  6. ImportError: cannot import name &#39;NUMPY_MKL&#39;
  7. sentinel.conf配置
  8. zoj 2110
  9. 删除sql计划 调用的目标发生了异常。 (mscorlib) 其他信息: 用户 &#39;sa&#39; 登录失败。
  10. Symbol() 的使用方法
  11. 栈stack(2):栈的链表实现
  12. python脚本批量生成数据
  13. JAVA API操作hbase1.4.2
  14. Spring py登陆模块(包含 记录登陆时间,记录ip,增加积分)
  15. [vue开发记录]全局loading组件
  16. Cocos Creater 监听程序到后台和重新到前台
  17. python3 写的一个压测脚本(有待开发)
  18. 基于ARM9和嵌入式Linux系统的多功能综合通信控制系统的框架
  19. IDEA项目搭建四——使用Mybatis实现Dao层
  20. Learning to Rank算法介绍:RankNet,LambdaRank,LambdaMart

热门文章

  1. 支持flv的播放神器
  2. Oracle 数据库分页查询的三种方法
  3. 【剑指offer】面试题 15. 二进制中 1 的个数
  4. LOJ #6285. 数列分块入门 9-分块(查询区间的最小众数)
  5. 如何判断图中存环(正&amp;负)
  6. python3.6下安装结巴分词需要注意的地方
  7. Number Sequence HDU - 5014
  8. POJ 2960 S-Nim 博弈论 sg函数
  9. 网络编程之tcp五层模型
  10. (原创)Stanford Machine Learning (by Andrew NG) --- (week 6) Advice for Applying Machine Learning &amp; Machine Learning System Design