Mapper not initialized. Call Initialize with appropriate configuration.

來源:互聯網
上載者:User

標籤:method   ppi   pre   sse   for   show   isa   epo   studio   

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!

c# asp.net-mvc unit-testing automapper
shareimprove this question edited Aug 17 ‘16 at 15:30Nkosi33.7k92864 asked Aug 17 ‘16 at 14:42user19906010
 
    
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 
add a comment
1 Answeractiveoldestvotes
up vote3down 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.

Mapper not initialized. Call Initialize with appropriate configuration.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.