views:

65

answers:

2

I have a class like so:

public abstract class ClassA<T>
{
    protected ClassA(IInterface interface)
    {
    if (interface== null)
            {
            throw new ArgumentNullException ("interface");
            }
    }
}

I want to write a test which verifies that if I pass null in the exception is thrown:

[Test]
[ExpectedException (typeof (ArgumentNullException))]
public TestMethod()
{
    ClassA classa = MockRepository.GenerateMock<ClassA<String>> (null);
}

but the test keeps failing with an exception rather than the exception being expected. I also tried wrapping the call in a try catch block, but same issue. I tried GenerateStub and PartialMock.

What am I missing?

A: 

Did you try "if (!(interface instanceof IInterface))" ?

OcuS
interface couldn't not be an instance of IInterface or the code wouldn't compile as you would be passing an argument of the wrong type to the constructor.
Sam Holder
+1  A: 

I've recently run into this issue myself, unfortunately I haven't been able to find any way to tell Rhino not to wrap the exception itself. Thus far, the best I've been able to come up with would be as follows:

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestMethod()
{
    try
    {
        ClassA classa = _mocks.CreateMock<ClassA>(null);
    }
    catch (Exception e)
    {
        if (e.InnerException != null)
        {
            throw e.InnerException;
        }
    }
    finally
    {
        _mocks.ReplayAll();
    }
}