Understanding Why We Use Inversion of Control Containers for Unit Testing

I am currently looking into including Ninject in my unit tests. Looking through some very smart posts related to earlier questions ( What is Ninject and when do you use it ? , http://martinfowler.com/articles/injection.html ); I think I have some basic concepts, but I am struggling with some if its applications.

Most of the time IOCs are incorporated is because of their inclusion in your unit tests. However, as a practical matter, does it make sense to use a framework like Ninject in my unit tests if I haven't already included it in my actual codebase?

Example given below (Taken from development by James Bender with professional testing with C #: Developing Real World Applications with TDD)

[TestFixture]
public class PersonServiceTests
{
    [Test]
    public void ShouldBeAbleToCallPersonServiceAndGetPerson()
    {
        var expected = new Person {Id = 1, FirstName = "John", LastName = "Doe"};
        var kernel = new StandardKernel(new CoreModule());
        var personService = kernel.Get < PersonService > ();
        var actual = personService.GetPerson(expected.Id);
        Assert.AreEqual(expected.Id, actual.Id);
        Assert.AreEqual(expected.FirstName, actual.FirstName);
        Assert.AreEqual(expected.LastName, actual.LastName);
    }
}

      

As written my test using Ninject improves my life (or any bad developer who inherits my code), as opposed to just writing my test as:

[TestFixture]
public class PersonServiceTests
{
    [Test]
    public void ShouldBeAbleToCallPersonServiceAndGetPerson()
    {
        var expected = new Person {Id = 1, FirstName = "John", LastName = "Doe"};
        var personService = new PersonService();
        var actual = personService.GetPerson(expected.Id);
        Assert.AreEqual(expected.Id, actual.Id);
        Assert.AreEqual(expected.FirstName, actual.FirstName);
        Assert.AreEqual(expected.LastName, actual.LastName);
    }
}

      

Since I am including the principles of dependency inversion in my code; I see the importance of IOCs as a way to reduce the redundant / centralized code associated with how my classes are created (as a result of following the Dependency Inversion Principles). However, if I haven't bothered to use an IOC container in my code, is it helpful to use something like Ninject solely as part of my module framework block?

+3


source to share


1 answer


Why would you want to use DI frameworks in your tests? Are your graphics difficult to compose?

Personally, I think you'd be better off just making the tests explicit dependencies, since then mocks or stub injections would be explicit in tests and very noticeable.

It also hides any problems when creating complex dependency hierarchies. Creating all objects in tests means that you will have to feel the pain of creating these dependencies and motivate you to improve the design.



I don't think using the DI framework buys you anything in this situation other than obfuscating the dependencies your test has.

Integration tests can be different, but for unit tests ...

+5


source







All Articles