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. AC日记——采花 洛谷 P2056

    采花 思路: 莫队: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 int bel[maxn] ...

  2. java入门概念梳理总结

    Java入门学习 简介 public class HelloWorld { public static void main(String []args) { System.out.println(&q ...

  3. macos不能打开windows samba共享问题(转载)

    转自:https://www.macx.cn/thread-2095377-1-1.html?mod=viewthread&tid=2095377&extra=page%253D1&a ...

  4. 【笔试题】精选30道Java笔试题解答

    转自于:精选30道Java笔试题解答 精选30道Java笔试题解答 1. 下面哪些是Thread类的方法() A. start() B. run() C. exit() D. getPriority( ...

  5. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Apache配置文件相关命令

    转:http://www.365mini.com/page/apache-options-directive.htm Options指令是Apache配置文件中一个比较常见也比较重要的指令,Optio ...

  7. linux c 学习笔记

    gcc是linux c编译器 gcc -o o指定文件名或者会生成a.out文件 comment 注释 generate(生成) some simple ouput 使用标准 为C89

  8. POJ1958 Strange Towers of Hanoi [递推]

    题目传送门 Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3117   Ac ...

  9. redis缓存总结----干货

    Redis的概念 Redis是一款内存高速缓存数据库.Redis全称为:Remote Dictionary Server(远程数据服务),Redis是一个key-value存储系统,它支持丰富的数据类 ...

  10. Flask实战第48天:首页轮播图实现

    首页的布局如下 因为以后所有的内容都是在main-container里面,所以这里我们修改front_base.html,把{% block body%}{% endblock%}放到里面去 < ...