views:

56

answers:

1

I have a unit test invoking a constructor, passing in a "null" on purpose to test the handling of the null.

I expect the method invoked to throw an ArgumentNullException, but when I step through the code, I see the parameter has actually been initialised.

This has me stumped, although my gut says it has something to do with the DI container (Castle Windsor).

Can anyone shed any light on this?

My unit test, a null is passed together with an instantiated delegate:

 [Test]
 public void ConstructorThrowsAnExceptionWhenImplementationCollectionIsNull()
 {
     //assert
     Assert.Throws<ArgumentException>(() => new CacheImplementationSelector(null, _stubCacheImplementationSelectorDelegate));
 }

The invoked method:

public CacheImplementationSelector(ICollection<ICacheImplementation> implementations, CacheImplementationSelectorDelegate selectorDelegate)
{
    implementations.IsNotNullArgCheck("implementations");
    ...

Hovering my mouse over the implementations parameter with the code stopped on a breakpoint in the CacheImplementationSelectorMethod, visual studio tells me the parameter "implementations" has a Count of 1 and [0] is null.

I am using ReSharper to run the NUnit test.

For completeness the TestFixtureSetup and SetUp are as follows:

[TestFixtureSetUp]
public void FixtureSetUp()
{         
    _mocks = new MockRepository();
}

[SetUp]
public void Setup()
{
    _listOfImplementations = new List<ICacheImplementation>() { _stubICacheImplementation };            
    _stubCacheImplementationSelectorDelegate = MockRepository.GenerateStub<CacheImplementationSelectorDelegate>();
     _stubICacheImplementation = MockRepository.GenerateStub<ICacheImplementation>();
     _stubKeyCreator = MockRepository.GenerateStub<ICacheKeyCreator>();
     _stubStrategy = MockRepository.GenerateStub<ICachingStrategy>();
     _stubEncoder = MockRepository.GenerateStub<ICacheItemEncoder>();
     _c = new CacheImplementationSelector(_listOfImplementations, _stubCacheImplementationSelectorDelegate);
     _testObject = new object();
     _yesterday = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
     _tomorrow = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0));
     _testString = "test";
     _tooLongKey = "a".Repeat(Cache.MaxKeyLength+1);
     _tooLongFriendlyName = "a".Repeat(Cache.MaxFriendlyNameLength + 1);
}
+1  A: 

Is it possible that when you are stepping through the code you are seeing the execution of this line in the [SetUp] method?

_c = new CacheImplementationSelector(_listOfImplementations, _stubCacheImplementationSelectorDelegate);

That code would run before your unit test and the "implementation" method would not be null.

As for the unit test failing, can we see the implementation of the IsNotNullArgCheck method? I assume it's some extension method using some reflection. Maybe there is a bug in there?

celticpride
I asked the question when naiive about unit testing. Code presented is not enough to give full answer. Cannot confirm this answer was the solution, but is likely, hence acceptance.
Ben Aston