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();
}
}
精彩评论