Mapper not initialized. Call Initialize with appropriate configuration.

Source: Internet
Author: User

System.InvalidOperationException: "Mapper not initialized. Call Initialize with appropriate configuration. If you is trying to use mapper instances through a container or otherwise and make sure you do not have any calls to the STA Tic Mapper.map methods, and if you ' re using projectto or useasdatasource extension methods, make sure you pass in the APPR Opriate Iconfigurationprovider instance. "

Here's the code for the method, that I ' m developing, the unit test for:

Public ActionResult Itemslisting(){ VarItemslist=Itemsrepository.GetItems(True); If (Itemslist.Count() > 0) { VarItemslistvms= Mapper.Map<IEnumerable<item>,  Ienumerable<itemslistingviewmodel>> (itemsList< Span class= "pun");  return view ( itemslistvms);  } else  { return redirecttoaction  "Home" }}          /span>                

Following is the code from the mapping configuration file:

Public Static Class Mappingconfig{ public static void registermaps ()  { mapper. Initialize (config => { Config. Createmap<item,> ();  }}       /span>                

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

MappingConfig.RegisterMaps();

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

[TestMethod]Public void Itemslisting () { homecontroller< Span class= "PLN" > controller = new homecontroller (); viewresult result = controller itemslisting ()  as Span class= "Typ" >viewresult;assert. Isnotnullresult               /span>             

It works fine when I simply run the application. But if I try to run the unit test method, it shows the mentioned error message. Can anyone help me to get through this issue? thanks!

C # ASP.NET-MVC Unit-testing AutoMapper
shareimprove this question edited Aug 15:30 nkosi33.7k9 2864 asked aug +" at 14:42 User199060< Span class= "Badge3" >10
What test framework is you using, MSTest? –lukew ' at 14:48
@LukeW: Yes, it is MSTest. –user1990 ' at 14:50
1
@user1990, is calling in MappingConfig.RegisterMaps(); your unit tests?–nkosi "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 was not Application_Start() executed. It is associated with IIS, which are not running during unit tests. Manually call the mapping configurations.

[TestClass]Public Class Homecontrollertests { [ClassInitialize] Public Static void Init(TestContextContext) { Mappingconfig.Registermaps(); } [TestMethod] Public void Itemslisting() { homecontroller controller = new homecontroller< Span class= "pun" > ();  viewresult result =. Itemslisting ()  as  Viewresult; assert. Isnotnullresult}}          /span>                

In the above test the mapping configuration was done in a method decorated with [ClassInitialize] attribute which

Classinitializeattribute Class identifies a method that contains code this must be used before all of the tests in the TES The T class has the run and to allocate resources to being used by the test class.

Mapper not initialized. Call Initialize with appropriate configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.