0
votes

I have a solution with multiple projects and one of these projects is my service class which calls into the persistence manager.

I would like to write a unit test as follows:

 [Test]
    public void Create_HappyPath_Success()
    {
        // Arrange
        UnitOfMeasure unitOfMeasure = new UnitOfMeasure();
        unitOfMeasure.Code = "Some new unit of measure";
        unitOfMeasure.DataOwner = 1;

        // Act
        this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

        // Assert something

    }

Now, I'm getting a null reference exception on this line:

 this.UoMService.Create(unitOfMeasure);  // Fails here as UoMService is null

I believe that it's due to the fact that Castle Windsor is not getting called and hence the UoMService isn't getting instantiated. My Castle Windsor application installer is defined in another project i.e. my ASP.NET MVC project. So my first question is whether it's possible to reuse that installer to run my Unit Tests.

Now to get around this problem, I created a new installer in my unit test project by linking to the installer in my web project. Then I used the following code in my set up:

  [SetUp]
    public void ControllersInstallerTests()
    {
        this.containerWithControllers = new WindsorContainer();
        IoC.Initialize(this.containerWithControllers);

        this.containerWithControllers.Install(FromAssembly.This());
    }

This time when I run the tests, I get the following error:

SetUp : Castle.Windsor.Configuration.Interpreters.XmlProcessor.ConfigurationProcessingException : Error processing node resource FileResource: [] [] ----> Castle.Core.Resource.ResourceException : File C:\Projects\DavidPM\Services\MyProject.Services.ServiceImpl.Test.Unit\bin\Debug\Config\Windsor.config could not be found

The question is why is it looking in the bin\Debug folder?

As a newbie with Castle Windsor, I am not sure what I should be doing to hook into Castle Windsor for my unit tests.

2

2 Answers

1
votes

You should not be hooking up your IoC container in your unit tests. During production, your IoC container will resolve dependencies. During unit tests, you create the dependencies as part of your tests -- usually using a mocking framework so you can test in isolation.

0
votes

make your config file copy to output directory