#31 Can't mock interface with ToString method

open
nobody
nmock2 (11)
5
2008-08-07
2008-08-07
Anonymous
No

I'm new to NUnit/NMock and TDD/Mocking in general, so this may be something stupidly simple that I've overlooked. I get an exception any time I try to mock an interface containing the ToString() method, even if the test itself has nothing to do with the ToString() function. If I comment out this method from the interface, the exception goes away. Any assistance you can provide would be appreciated.

Steps to reproduce the problem:
1) Create a new VB.NET Class Library project in VS2008
2) Add a reference to nunit.framework v2.4.8.0 and NMock2.dll 2.0.0.44
3) Add an interface containing a Function ToString() As String method
4) Write a test that mocks that interface.
5) Compile
6) Run the test in Nunit GUI

Here's the exception that is thrown:

System.InvalidProgramException : Common Language Runtime detected an invalid program.

Server stack trace:
at MockObjectType1.ToString()

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at System.Object.ToString() at NMock2.Internal.ExpectationBuilder.On(Object receiver) at NMockTest.EntityTest.ReproduceCLRExceptionTest() in C:\Documents and Settings\jdentler\My Documents\Visual Studio 2008\Projects\NMockTest\NMockTest\Test.vb:line 20

Here's my code to reproduce the problem:

Imports NUnit.Framework
Imports NMock2

Public Interface IEntity

Function ToString() As String
ReadOnly Property SomethingUnrelated() As Integer

End Interface

<TestFixture()> _
Public Class EntityTest

<Test()> _
Public Sub ReproduceCLRExceptionTest()

Dim M As New NMock2.Mockery
Dim aMock As IEntity = M.NewMock(Of IEntity)()
Expect.Once.On(aMock).GetProperty("SomethingUnrelated").Will([Return].Value(1))
Assert.IsTrue(aMock.SomethingUnrelated = 1)
M.VerifyAllExpectationsHaveBeenMet()

End Sub

End Class

Discussion