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:

  1. public ActionResult ItemsListing()
  2. {
  3. var itemsList = itemsRepository.GetItems(true);
  4. if (itemsList.Count() > 0)
  5. {
  6. var itemsListVMs = Mapper.Map<IEnumerable<Item>, IEnumerable<itemsListingViewModel>>(itemsList);
  7. return View(itemsListVMs);
  8. }
  9. else
  10. {
  11. return RedirectToAction("Home");
  12. }
  13. }

Following is the code from the mapping configuration file:

  1. public static class MappingConfig
  2. {
  3. public static void RegisterMaps()
  4. {
  5. Mapper.Initialize(config =>
  6. {
  7. config.CreateMap<Item, itemsListingViewModel>();
  8. });
  9. }
  10. }

And I have initialized mapper in the Application_Start() event of the Global.asax as below:

  1. MappingConfig.RegisterMaps();

Below is the simple test method that I'm trying to run:

  1. [TestMethod]
  2. public void ItemsListing()
  3. {
  4. HomeController controller = new HomeController();
  5. ViewResult result = controller.ItemsListing() as ViewResult;
  6. Assert.IsNotNull(result);
  7. }

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.

  1. [TestClass]
  2. public class HomeControllerTests {
  3. [ClassInitialize]
  4. public static void Init(TestContext context) {
  5. MappingConfig.RegisterMaps();
  6. }
  7. [TestMethod]
  8. public void ItemsListing() {
  9. HomeController controller = new HomeController();
  10. ViewResult result = controller.ItemsListing() as ViewResult;
  11. Assert.IsNotNull(result);
  12. }
  13. }

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.

Mapper not initialized. Call Initialize with appropriate configuration.的更多相关文章

  1. 【问题】关于Mapper not initialized的问题

    ERROR -- ::, [ ] nHandling.AbpApiExceptionFilterAttribute - Mapper not initialized. Call Initialize ...

  2. ABP+AdminLTE+Bootstrap Table权限管理系统第七节--登录逻辑及abp封装的Javascript函数库

    经过前几节,我们已经解决数据库,模型,DTO,控制器和注入等问题.那么再来看一下登录逻辑.这里算是前面几节的一个初次试水. 首先我们数据库已经有的相应的数据. 模型和DTO已经建好,所以我们直接在服务 ...

  3. BDD实战篇 - .NET Core里跑Specflow - 可以跑集成测试和单元测试

    这是<如何用ABP框架快速完成项目 >系列中和DevOps系列文章其中一篇文章.   BDD很赞!比TDD先进很多,能够大大提高编码效率.   上一篇文章说了如何在.NET Core里安装 ...

  4. ABP+AdminLTE+Bootstrap Table权限管理系统第七节--登录逻辑及几种abp封装的Javascript函数库

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期         简介 经过前几节,我们已经解决数据库,模型,DTO,控制器和注入等问题.那么再来看一下登录逻辑.这 ...

  5. .NET Core 中依赖注入 AutoMapper 小记

    最近在 review 代码时发现同事没有像其他项目那样使用 AutoMapper.Mapper.Initialize() 静态方法配置映射,而是使用了依赖注入 IMapper 接口的方式 servic ...

  6. Document root element "configuration", must match DOCTYPE root "mapper".

    最近剛剛鼓搗mybatis , 第一個demo就出了問題.其實原因是因為將mapper中的頭copy到了configuration里去了 <?xml version="1.0" ...

  7. WARN [main] conf.HiveConf (HiveConf.java:initialize(1488)) - DEPRECATED

    问题描述:hive 关于告警问题的解决:WARN  [main] conf.HiveConf (HiveConf.java:initialize(1488)) - DEPRECATED: Config ...

  8. myBatis源码之Configuration

    Configuration类主要是用来存储对mybatis的配置文件及mapper文件解析后的数据,Configuration对象会贯穿整个myabtis的执行流程,为mybatis的执行过程提供必要 ...

  9. Mybatis Mapper接口是如何找到实现类的-源码分析

    KeyWords: Mybatis 原理,源码,Mybatis Mapper 接口实现类,代理模式,动态代理,Java动态代理,Proxy.newProxyInstance,Mapper 映射,Map ...

随机推荐

  1. Maven的私有仓库Nexus

    1.什么是Nexus 在前面进行maven项目的构建中,可以看到在构建的过程中需要安装maven的依赖插件,如图: 在日常的开发构建中,我们也可以自己搭建一个私有的nexus.那么什么是nexus呢? ...

  2. Jsonp方式和httpclient方式有什么区别?

    jsonp基于js,解决跨域问题,本质发起ajax情求但是Jsonp只支持get请求. 它不安全,它先解析js,然后发起ajax请求,然后获取到返回值,通过浏览器返回,最后解析. JQuery和Spr ...

  3. 架构设计之NodeJS操作消息队列RabbitMQ

    一. 什么是消息队列? 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象. 消息队列(Message Queue)是一种应用间的通信 ...

  4. 使用 ceph 作为 openstack 的后端

    openstack 与 ceph 集成 在 ceph 上创建 openstack 需要的 pool. sudo ceph osd pool create volumes 128 sudo ceph o ...

  5. SpringMVC中为什么要配置Listener和Servlet

    一直以来,我们使用SpringMVC的时候习惯性都配置一个ContextLoaderListener,虽然曾经有过疑问,配置的这个监听器和Servlet究竟做了什么,但也没深究. 要说任何Web框架都 ...

  6. 洛谷——P4018 Roy&October之取石子

    P4018 Roy&October之取石子 题目背景 Roy和October两人在玩一个取石子的游戏. 题目描述 游戏规则是这样的:共有n个石子,两人每次都只能取p^kpk个(p为质数,k为自 ...

  7. ubuntu问题集锦

    我使用的是ubuntu 14.04 用UltraIOS 制作镜像安装的  ubuntu 问题1:闪屏问题以及文字显示不全 解决方案:重装显卡驱动 解决过程:http://my.oschina.net/ ...

  8. 【BZOJ 1095】 1095: [ZJOI2007]Hide 捉迷藏 (括号序列+线段树)

    1095: [ZJOI2007]Hide 捉迷藏 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏 ...

  9. 有的系统区apk需要对其系统签名,才能正常使用。

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 向方案公司索要platform.x509.pem 和platform.pk8这两个文件. ...

  10. [Arc074E] RGB Sequence

    [Arc074E] RGB Sequence Description 今天也在愉快地玩Minecraft!现在MM有一块1?N的空地,每个格子按照顺序标记为1到N.MM想要在这块空地上铺上红石块.绿宝 ...