开发者

How can I test for null arguments in the constructor of abstract class using rhino mocks?

开发者 https://www.devze.com 2022-12-16 17:06 出处:网络
I have a class like so: public abstract class ClassA<T> { protected ClassA(IInterface interface) { if (interface== null)

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?


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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消