creating a mock of an IDisposable object

1,121 views
Skip to first unread message

Rudi

unread,
Aug 4, 2009, 3:32:26 AM8/4/09
to NUnit-Discuss
Hi,

I'm using the Mocking Framework from Nunit (2.5.1). When I create a
DynamicMock object from a class that implements IDisposable, I get an
argumentException when trying to get the MockInstance property back.

I have found this to be a problem that a lot of other mocking
frameworks have had, and most of them have solved it. But at this
point we do not want to introduce another mocking framework, but would
like to continue to use the nunit mocks framework.

I guess the obvious workaround is to create an Interface for the
object and mock the interface instead of the IDisposable object.

(I'm working with legacy code here...:-(

repro:
using NUnit.Framework;
using NUnit.Mocks;
public class TestMe : IDisposable
{
#region IDisposable Members

public void Dispose()
{
throw new Exception("The method or operation is not
implemented.");
}

#endregion
}
[TestFixture]
public class TestClass
{
[Test]
public void TestMeTest()
{
DynamicMock mocked = new DynamicMock(typeof(TestMe));
TestMe test = mocked.MockInstance as TestMe; //
exception thrown here

}
}

TestClass.TestMeTest:
System.ArgumentException : classToProxy argument must derive from
MarshalByRef type.

RJV

unread,
Aug 5, 2009, 1:47:33 AM8/5/09
to nunit-...@googlegroups.com
Hi Rudi,
 
If the Class Under Test derives from MarshalByRef then the exception is not thrown. Does this solve your query?
 
using System;
using System.Runtime.Remoting.Proxies;
using NUnit.Framework;
using NUnit.Mocks;
namespace NUnit_Mock_ClassLibrary
{
    public class TestMe : MarshalByRefObject, IDisposable
    {
        public void Dispose()
        {
            throw new NotImplementedException();// ("The method or operation is not implemented.");

        }
    }
    [TestFixture]
    public class TestClass
    {
        [Test]
        [ExpectedException("NotImplementedException")]

        public void TestMeTest()
        {
            DynamicMock mocked = new DynamicMock(typeof(TestMe));
            TestMe test = mocked.MockInstance as TestMe;    // exception not thrown here if class under test derives from MarshalByRef
            Assert.Throws(typeof(NotImplementedException),(()=>test.Dispose()));
        }
    }
}
 
But the test fails with the above code as no exception is thrown!
 
Regards,
 
Jv

 

Charlie Poole

unread,
Aug 5, 2009, 7:31:56 AM8/5/09
to nunit-...@googlegroups.com
Hi Rudi,

As I think you have figured out, NUnit Mocks only work with
interfaces or MarshalByRef classes. That's because the underlying
technology we use - creating a remoting proxy - only works with
those cases.

NUnit Mocks was created over a weekend as a toy mock implementation,
other "real" mock frameworks use different approaches to creation
of the mock object so that they can work with classes in general.

We have kept NUnit Mocks alive for a long time because it seemed
like a good way to introduce people to Mocks, but have always
warned that it was not a suitable platform for production work.
I'm beginning to think that was a mistake, because you are far
from the first person to become reliant on it.

We'll be discussing what to do with NUnit Mocks in NUnit 3.0. I
think that one likely outcome is to eliminate it. So I suggest
that you use an interface as a workaround and consider converting
to one of the excellent mocking frameworks like NMock2, Rhino or Moq.

Charlie

Charlie Poole

unread,
Aug 5, 2009, 7:34:03 AM8/5/09
to nunit-...@googlegroups.com
Hi Jv,
 
Deriving from MarshalByRef has significant impacts on the application and can't be done
for types that already derive from something else.
 
Creating an interface that is not used except by the mock has almost no impact.
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of RJV
Sent: Tuesday, August 04, 2009 10:48 PM
To: nunit-...@googlegroups.com
Subject: [nunit-discuss] Re: creating a mock of an IDisposable object

RJV

unread,
Aug 5, 2009, 7:36:17 AM8/5/09
to nunit-...@googlegroups.com
Thanks, Charlie. Can you please tell me as to why the exception does not get thrown from the Dispose() method? I am not able to figure out.
 
Jv

 

Charlie Poole

unread,
Aug 5, 2009, 8:53:40 AM8/5/09
to nunit-...@googlegroups.com
Because nobody is calling Dispose, I imagine. :-)
 
Charlie


Sent: Wednesday, August 05, 2009 4:36 AM

Andy McMullan

unread,
Aug 5, 2009, 9:38:13 AM8/5/09
to nunit-...@googlegroups.com
> Thanks, Charlie. Can you please tell me as to why the exception does not get
> thrown from the Dispose() method? I am not able to figure out.

Presumably your test should use Assert.Throws or ExpectedException,
but not both. I haven't used Assert.Throws, but I assume it swallows
the exception, and therefore the ExpectedException condition is not
met and your test fails.

Charlie Poole

unread,
Aug 5, 2009, 12:34:52 PM8/5/09
to nunit-...@googlegroups.com
Hi Andy,

I'm afraid I was looking at the OP's example rather than Jv's when
responding to Jv. Now that I see what he meant, you are of course
correct. Assert.Throws generates no exception when it is successful
and AssertionException when it fails.

Of course, the whole idea of Assert.Throws and the ThrowsCOnstraint
is to get rid of ExpectedException as an approach due to its
inherent limitations.

Charlie

> -----Original Message-----
> From: nunit-...@googlegroups.com
> [mailto:nunit-...@googlegroups.com] On Behalf Of Andy McMullan
> Sent: Wednesday, August 05, 2009 6:38 AM
> To: nunit-...@googlegroups.com
> Subject: [nunit-discuss] Re: creating a mock of an IDisposable object
>
>

RJV

unread,
Aug 6, 2009, 4:09:23 AM8/6/09
to nunit-...@googlegroups.com
I had observed this behavior when using both (attribute and Asserts) but forgot to uncomment the attribute when posting here but the exception is not thrown. Any help?

Assert

.Throws(typeof(System.NotImplementedException),(()=>test.Dispose()));Jv

 

Andy McMullan

unread,
Aug 6, 2009, 4:29:33 AM8/6/09
to nunit-...@googlegroups.com
> I had observed this behavior when using both (attribute and Asserts) but
> forgot to uncomment the attribute when posting here but the exception is not
> thrown. Any help?

Looking at your code again, why would you expect the exception to be
thrown? You're calling the method on the mock object, not the real
object, and it's the real object that throws the exception. The mock
will only throw an exception if you tell it to.

RJV

unread,
Aug 6, 2009, 4:48:09 AM8/6/09
to nunit-...@googlegroups.com
Ok. Thanks, btw, it is not my code:)
 
Jv

 

Rudi

unread,
Aug 7, 2009, 5:09:56 AM8/7/09
to NUnit-Discuss
Hi all,

Thanks for the replies. I did indeed find a post somewhere explaining
the function and limited implementation for the nunit mocks. To bad
there is no page on the website on the nunit mocks. That could save
many new users of nunit the time in finding out what I just did.

But congrats anyway as it did allow me to get started with the nunit
mocks and can now start to move to NMock or Rhino Mocks to go further
if I need to.

I did in the end create an interface for that class (it was in dire
need of one anyway for other reasons).

Thanks!!

Charlie Poole

unread,
Aug 7, 2009, 6:25:16 AM8/7/09
to nunit-...@googlegroups.com
Hi Rudi,

Glad it worked out anyway.

Your point about finding info is a good one. There are a few
areas where the only real info is in announcements and the
subsequent discussions - all transient and hard to find for
those who come new to NUnit. Hopefully, we can start collecting
this stuff in the FAQ that Kelly is setting up.

Charlie

> -----Original Message-----
> From: nunit-...@googlegroups.com
> [mailto:nunit-...@googlegroups.com] On Behalf Of Rudi
> Sent: Friday, August 07, 2009 2:10 AM
> To: NUnit-Discuss
> Subject: [nunit-discuss] Re: creating a mock of an IDisposable object
>
>
Reply all
Reply to author
Forward
0 new messages