shekharpro / mb-unit

Automatically exported from code.google.com/p/mb-unit
0 stars 0 forks source link

ExpectedArgumentNullException matching problem #391

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It seems that ArgumentNullExceptions are not correctly detected if they are
thrown from a partial mock made with Rhino.Mocks.

Class under test:

public abstract class SomeClass {
  protected SomeClass(string value) {
    if (value == null) {
      throw new ArgumentNullException("value");
    }
  }
}

Unit test using Rhino.Mocks and MbUnit 3:

[Test, ExpectedArgumentNullException]
public void CtorShouldThrowIfArgumentIsNull() {
  var mocks = new MockRepository();
  mocks.PartialMock<SomeClass>(default(string));
}

Expected result: pass
Actual result: test fails

TestCase 'MbUnit
v3.0.5.546/GallioError/PartialMockTests/CtorShouldThrowIfArgumentIsNull'
failed: 
    Expected Exception
    Expected an exception of type 'System.ArgumentNullException' but a
different exception was thrown.
    System.Exception: Exception in constructor: System.ArgumentNullException:
Der Wert darf nicht NULL sein.

Running the same test with MbUnit 2.4 passes.

Original issue reported on code.google.com by m...@andreloker.de on 14 Feb 2009 at 12:56

GoogleCodeExporter commented 8 years ago
It looks like Rhino.Mocks is wrapping the original exception inside of a
System.Exception itself.  So MbUnit isn't actually seeing the underlying
ArgumentNullException.

I hesitate to change the behavior to search inside inner exceptions because 
that has
other undesirable side-effects on the API such as with Assert.Throws<> which 
returns
the exception that was actually caught.  Would be confusing if it ended up 
returning
an inner exception.

Try this:

var ex = Assert.Throws<Exception>(() => mocks.PartialMock<SomeClass>(null));
Assert.IsInstanceOfType<ArgumentNullException>(ex.InnerException);

Original comment by jeff.br...@gmail.com on 19 Feb 2009 at 4:56

GoogleCodeExporter commented 8 years ago

Original comment by jeff.br...@gmail.com on 19 Feb 2009 at 5:03

GoogleCodeExporter commented 8 years ago
Thanks for the reply. I'll give the suggested work around a try, although it is
strange that MbUnit 2.4 worked flawlessly with Rhino.Mocks.

Original comment by m...@andreloker.de on 20 Feb 2009 at 3:14

GoogleCodeExporter commented 8 years ago
Hi,

Same problem here. We have made lots of tests to check that our classes 
correctly 
throw ArgumentNullException when a null parameter is passed to the constructor. 
They 
are now all failing since we migrated to MbUnit 3.

Original comment by charles....@gmail.com on 19 Mar 2009 at 2:48

GoogleCodeExporter commented 8 years ago
Maybe you could try to convince Oren to use a method like
ExceptionUtils.RethrowWithNoStackTraceLoss.

Alternately, Yann recently added an overload of Assert.Throws that takes an 
inner
exception type like this: Assert.Throws<Exception, ArgumentNullException>(() => 
...);

Original comment by jeff.br...@gmail.com on 4 Dec 2009 at 4:50