0

I've used AutoMapper in my controller for mapping my Model objects to my ViewModel objects. A IMappingEngine object is injected in my controllers by Unity and the Mapping configuration is done in the global.asax.

In my controller's unit tests, how should I mock the IMappingEngine passed as parameter in the constructor ?

Do I have to setup all the mapping configuration again in the unit test project ?

1
  • Can you add some sample code that illustrates your dilemma? That will make it easier to give you a good answer. Also, what mocking library are you using in your tests? Commented Jan 31, 2013 at 16:56

2 Answers 2

1

If you are abstracting AutoMapper through the use of an interface the point of this is so that you do NOT have to use AutoMapper in your unit test. It is a unit test so you are testing just the code in the controller action. All you need to do is use a mocking framework like RhinoMocks or Moq and mock the behaviour you need for your test. With RhinoMocks you would have something like this:

var mapper= MockRepository.GenerateStub<IMappingEngine>();

and then use mapper.Stub to add the behaviour you need.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, that's seem to be what I'm looking for. However I'm a bit confused with the mapper.Stub part... Do I basically pass it the output that I would normally expect from Automapper, so that the controller can be tested without automapper ?
Exactly right, you make the stub perform like you would expect AutoMapper to normally behave.
0

Why are you injecting AutoMapper in the first place?

What are your tests suppose to test since the output of your controller (ViewModels) is dependent on the mappings?

I suspect you have a bit too much abstractions in your code. There is nothing wrong with using the AutoMapper directly.

1 Comment

Probably it's to avoid using Mapper as a static class when mapping, which seems to have been one of the regrets of the AutoMapper's creator and marked as obsolete. lostechies.com/jimmybogard/2016/01/21/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.