Wednesday, November 5, 2014

Using different containers with Chill

When Chill 1.0 wanted to know which container (for example the Chill.AutofacNSubstitute container) it needed to use, it would scan your loaded assemblies for types that implemented the IChillContainer. While this worked, it also posed several issues.

For one, you couldn’t use different types of chill containers. For example, if you wanted to have tests with several containers that do automocking, some without automocking, some with very specific registrations set up), you couldn’t do that.

The other issue had to do with portable class libraries. I wanted to make Chill a portable class library. For example if you wanted to test Xamarin IOS classes (without turning them into PCL’s), then you can only reference PCL’s or Xamarin IOS dll’s from your test project.

But, to turn Chill into a PCL, I had to change the way it did assembly scanning. PCL’s have quite a lot of restrictions with regards to assembly scanning.

However, I think the end result is quite elegant.

Basically, if you install a specific container, Chill will add an assembly level attribute, in a file called DefaultChillContainer:

// This attribute defines which container will be used by default for this assembly

[assembly: ChillContainer(typeof(AutofacNSubstituteChillContainer))]


This tells Chill to use this container (in this case an AutoMocking container using NSubstitute) as the default container. Now, if you want to use a different container in some situations, you can override this on a per test basis:


public class When_configuring_container_with_module_and_objectmother : GivenWhenThen

Under the covers, Chill will wrap the container you specify in a AutoMotherDecorator (more on AutoMothers in a later blog post). If you don’t want this, then you can specify a custom ChillContainerInitializer:



The DefaultChillContainerInitializer is the class that wraps the ChillContainer the AutoMotherDecorator, so you can implement your own class if you don’t like this behavior. Now I don’t expect that this happens often, but it’s nice to have the option.

As always, keep it cool, keep it Chill!

1 comment: