Question

Running this code:

_foo = MockRepository.GenerateStub<IBar>();
_foo.Stub(x => x.Foo()).Return("sdf");

When

public interface IBar
{
   string Foo();
}

public class Bar : IBar
{
   public string Foo()
   {
      throw new NotImplementedException();
   }
}

throws NotSupportedException - "Can't create mocks of sealed classes". I understand why you can't mock a sealed class (although there are solutions in TypeMock), but what's the problem with mocking a class that returns a sealed class (string) ?

Was it helpful?

Solution

Rhino Mocks appears to be catching and handling this exception. You only see it in the VS.NET Debugger if you've enabled exceptions as breakpoints. It appears that you can safely continue running past this exception breakpoint and it all works as expected.

OTHER TIPS

This happens when you have NOT got "Just my code" enabled under Tools->Options->Debugging->General, and you have CLR exceptions, "Thrown" selected under Debug->Exceptions. Easiest way to fix it is enable "just my code" under Tools->Options->Debugging->General.

enter image description here

Your code works properly. You likely have some other code not shown which is causing the problem. Post your whole unit test here and we'll diagnose the issue for you.

I have the same problem, it has to be some VS studio debug setting or some insufficient access rights for rhino mocks i guess. I am pretty sure that its not the code that is causing this.

I second that - It's not an issue with code. It is VS debug setting.

I get the same exception while on debug on the code below, while trying to send Arg.Is.Anything as a parameter to a stub.

mockPermissionManager.Stub(item => item.HasAccess(Arg<string>.Is.Anything)).Return(true);

The exception is handled and code/mocking works as expected, just do F5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top