开发者

How to call a configuration method situated in my global.asax in my Test Project?

In my Global.asax **Application_Start()** I have a configuration of AutoMapper, this configuration is triggered whenever the application run.

I'm using AutoMapper in my Controllers.

I have a Test Project to test m开发者_如何学Cy Controllers and I need to trigger this configuration of AutoMapper whenever my test project is fired.

There is a place in my Test Project where I have something like the Application_Start() in Global.asax to call this Configuration Method of AutoMapper from there?


Try to add initialization method to test class and set [TestInitialize()]-attribute to it.

namespace TestNamespace
{
   [TestClass()]
   public class TestClass
   {
      [TestInitialize()]
      public void Initialize()
      {
        // some initialization code
      }
  }
}


For those of us still looking for another option to do this.

  • In the App_Start folder, add a class file AutoMapperConfig.cs

    namespace MyNameSpace
    {
      public class AutoMapperConfig
      {
        public static void Register()
        {
          // Your AutoMapper configuration should go in here
        }
      }
    }    
    
  • In global.asax Application_Start() method add

    AutoMapperConfig.Register();
    
  • Now in your test class you should be able to call the same configuration method (you will need to reference the mvc project).

    [TestMethod]
    public void AutoMapper_Configuration_Should_Succeed()
    {
      AutoMapperConfig.Register();
      Mapper.AssertConfigurationIsValid();
    }
    
  • Or (If you have more than one tests for which the configuration is needed)

    [TestInitialize()]
    public void Initialize()
    {
      AutoMapperConfig.Register();
    }
    
    [TestMethod]
    public void AutoMapper_Configuration_Should_Succeed()
    {
      Mapper.AssertConfigurationIsValid();
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜