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.

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. javascript实现与后端相同的枚举Enum对象

    ; (function (global, undefined) { global.Enum = function (namesToValues) { var enumeration = functio ...

  2. 微软企业库5.0 学习之路——第二步、使用VS2010+Data Access模块建立多数据库项目

    现在我就开始进入学习之路的第二步——Data Access模块,这个模块是企业库中被使用频率最高的模块,它很好的封装了数据库操作应用,为我们进行多数据库系统开发提供了便利,只需更改配置文件就 可以很快 ...

  3. App Center编译React Native平台Android应用

    做React Native一段时间后,对于React Native的发布有一些了解,原本的方法都是在本地直接生成APK文件的,具体可以参考<react native 生成APK> 因为需要 ...

  4. [putty] ubuntu 通过配置文件设置字体

    创建了一个session之后,就能在 ~/.putty/sessions/ 文件夹下看到session的配置文件了 $ vim ~/.putty/sessions/session-name 搜索Fon ...

  5. 快速判断&求出区间相交的长度

    有两个区间A[a1,b1], B[a2,b2],判断这两个区间有没有交集.我们可以分为两种思维来判断: /** *思路就是如果两个区间不相交,那么最大的开始端一定大于最小的结束端 **/ if(max ...

  6. TarjanLCA学习笔记

    1.前言 首先我们介绍的算法是LCA问题中的离线算法-Tarjan算法,该算法采用DFS+并查集,再看此算法之前首先你得知道并查集(尽管我相信你如果知道这个的话肯定是知道并查集的),Tarjan算法的 ...

  7. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  8. 10 个常用的 es6 特性

    1. const  and let 除了函数作用域之外,增加了块级作用域和常量.const 定义的绑定不可以修改,let定义的绑定在{ }不能访问.之前的 var 如果不在函数作用域内,相当于定义了一 ...

  9. uva 10648(简单dp)

    Recently one of my friend Tarik became a member of the food committee of an ACM regional competition ...

  10. 【递归】【线段树】【堆】AtCoder Regular Contest 080 E - Young Maids

    给你一个1~n的排列p,n是偶数,每次从中任选一对相邻的数出来,插到排列q的开头,如此循环,问你所能得到的字典序最小的排列q. 我们先确定q开头的两个数q1,q2,q1一定是p的奇数位的最小的数,而q ...