This is a migrated thread and some comments may be shown as answers.

Setup Mocking on AutoMapper fails

3 Answers 182 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
DenisCL
Top achievements
Rank 1
DenisCL asked on 15 Dec 2011, 12:09 PM
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.

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 16 Dec 2011, 04:25 PM
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 >>

0
DenisCL
Top achievements
Rank 1
answered on 16 Dec 2011, 05:18 PM
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.
0
Ricky
Telerik team
answered on 20 Dec 2011, 01:18 PM
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 >>

Tags
General Discussions
Asked by
DenisCL
Top achievements
Rank 1
Answers by
Ricky
Telerik team
DenisCL
Top achievements
Rank 1
Share this question
or