2

Possible Duplicate:
Using IoC for Unit Testing

I am refactoring some tests and the solution uses Ninject for DI via the MS ServiceLocator. There is a separate Ninject module for the tests but in using it the testing code becomes quite difficult to follow.

Do you use DI within your tests or should each test be focussed on creating objects it knows exist? i.e. being more explicit.

Community
  • 1
  • 1
Tim Peel
  • 2,274
  • 5
  • 27
  • 43

1 Answers1

4

Well, while highly subjective, I personally dont use a dependency injection framework when unit testing. Since I use Moq, I typically create Mock<Whatever> classes and then manually inject the dependencies myself. That way, I can actually mock the results and behavior quickly and easily without worrying what the framework is injecting.

Tejs
  • 40,736
  • 10
  • 68
  • 86
  • 1
    +1: The DI Framework could come in handy with integration tests, but mocking is best for unit tests. You should be able to read a unit test and know exactly what inputs, outputs, and service behaviors to expect. – StriplingWarrior Jun 01 '11 at 00:08