Setup Mocking on AutoMapper fails

4 posts, 0 answers
  1. DenisCL
    DenisCL avatar
    69 posts
    Member since:
    Dec 2009

    Posted 15 Dec 2011 Link to this post

    Hello,

    I'm trying to use SetupStatic or Initialize on AutoMapper.Mapper class (http://automapper.org/) and JustMock fails to setup the static class.
    You can download the very simple test at http://www.2shared.com/file/tECy5No-/MockInitializeAutoMapperTest.html

    Thanks for your help.

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 16 Dec 2011 Link to this post

    Hi Denis,

    Thanks again for sending the issue. I did found the issue with SetupStatic / Initialize. However, the reason why SetupStatic on Mapper class is falling is because of the following method:

    public void AddProfile<TProfile>() where TProfile : Profile , new();


    Basically, it contains a DefaultConstructorConstaint but the constructor in Profile class is protected, which means that it should be invoked by an implement (For example. DummyProfile :Profile) which JM does not know yet during initialization and thus can be mocked by lazy initialization:
    Mock.Arrange(() => Mapper.AddProfile<DummyProfileImplemented>()).DoNothing().MustBeCalled();
     
    DummyMapper.AddProfile<DummyProfileImplemented>();
     
    Mock.Assert(() => Mapper.AddProfile<DummyProfileImplemented>(), Occurs.Once());

    Here to note that it is not required to use Mock.SetupStatic { T } unless you are doing strict mock or want to mock the static constructor via StaticConstructor.Mocked. The method will be intercepted anyway during Mock.Arrange if not already.

    However, it should not throw the exception for non-public type rather put it up for lazy initialization. Therefore, if you need the fix for it; please create a support ticket and I will send you the internal build. Also, adding some points to your account for reporting it.

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. DenisCL
    DenisCL avatar
    69 posts
    Member since:
    Dec 2009

    Posted 16 Dec 2011 Link to this post

    Thanks for your reply.

    Si if I understand well, without the fix,  I need not to call Initialze() but instead call Arrange with the AddProfile Method.
    My final goal is to use the Map<TSource, TDestination>(obj) method, so I can mock my automappings (for example in a service method to return a DTO from my domain object).
    By the way, I also have to test the real implementation of Map<TSource, TDestination>(obj) in a class that specifically test my map.
    So my problem is how to do it in a way that all thoses tests will pass in an "run all tests" scenario. Like a build.

    Thank you for your support.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 20 Dec 2011 Link to this post

    Hi Denis,

    Thanks again for your prompt reply.

    Yes generally you don’t have to use Mock.Initialize or Mock.SetupStatic for a static method. Mock.Arrange does this if not already intercepted. However, when you a have scenario where in some tests you will be invoking mocked Map<TSource, TDestination>(obj) and in others you call it directly, you can specifically initialize that particular method by using Mock.Partial

    More information on this can be found at this post:

    http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

    Hope this answers your question.

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top