Error Mocking Multiple Generic Interfaces

3 views
Skip to first unread message

jasonm...@gmail.com

unread,
Jan 21, 2008, 9:34:43 AM1/21/08
to Rhino.Mocks
Problem:
Generic types get "mixed up" when mocking.

Failed Assertion:
Method
IGenericListFactoryProxy0f17377417ef4f86982cdc1648690e28.CreateAGenericList:
type argument 'System.String' violates the constraint of type
parameter 'OfType'.

Tests:
using System.Collections.Generic;
using MbUnit.Framework;
using Rhino.Mocks;
using Test.App;

namespace Test.Test
{
[TestFixture]
public class FooTest
{
private MockRepository mockery;
private IGenericListFactory mockFactory;

[SetUp]
public void Setup()
{
mockery = new MockRepository();
mockFactory = mockery.DynamicMock<IGenericListFactory>();
}

[Test]
public void Should_create_a_generic_list_of_strings()
{
IList<string> mockList =
mockery.DynamicMock<IList<string>>();
using (mockery.Record())
{

Expect.Call(mockFactory.CreateAGenericList<string>()).Return(mockList);
}

using (mockery.Playback())
{
Assert.AreEqual(mockList,
CreateSUT().GetAListOfTypeString());
}
}

private Foo CreateSUT()
{
return new Foo(mockFactory);
}
}
}

using System.Data;
using MbUnit.Framework;
using Rhino.Mocks;
using Test.App;

namespace Test.Test
{
[TestFixture]
public class BarTest
{
private MockRepository mockery;
private IConstrainedGenericFactory mockFactory;

[SetUp]
public void Setup()
{
mockery = new MockRepository();
mockFactory =
mockery.DynamicMock<IConstrainedGenericFactory>();
}

[Test]
public void Should_create_a_new_data_table()
{
DataTable mockTable = mockery.DynamicMock<DataTable>();
using (mockery.Record())
{

Expect.Call(mockFactory.CreateANewGenericObject<DataTable>()).Return(mockTable);
}

using (mockery.Playback())
{
Assert.AreEqual(mockTable,
CreateSUT().CreateANewDataTable());
}
}

private Bar CreateSUT()
{
return new Bar(mockFactory);
}
}
}

Application Files:
using System.Collections.Generic;

namespace Test.App
{
public interface IGenericListFactory
{
IList<TypeOfList> CreateAGenericList<TypeOfList>();
}
}

namespace Test.App
{
public interface IConstrainedGenericFactory
{
OfType CreateANewGenericObject<OfType>() where OfType : new();
}
}

using System.Collections.Generic;

namespace Test.App
{
public class Foo
{
private readonly IGenericListFactory factory;

public Foo(IGenericListFactory factory)
{
this.factory = factory;
}

public IList<string> GetAListOfTypeString()
{
return factory.CreateAGenericList<string>();
}
}
}

using System.Data;

namespace Test.App
{
public class Bar
{
private readonly IConstrainedGenericFactory factory;

public Bar(IConstrainedGenericFactory factory)
{
this.factory = factory;
}

public DataTable CreateANewDataTable()
{
return factory.CreateANewGenericObject<DataTable>();
}
}
}

referenced assembles:
MbUnit 2.4
Rhino.Mocks 3.3

Shawn Neal

unread,
Jan 21, 2008, 2:20:59 PM1/21/08
to Rhino...@googlegroups.com
I threw your code into a test project and was able to run it without any errors using RhinoMocks (trunk) and MBUnit GUI 2.4.197.  I assume from your description of the problem I should have seen some sort of RhinoMocks constraint exception?

-Shawn

Fabian Schmied

unread,
Jan 22, 2008, 3:26:10 AM1/22/08
to Rhino...@googlegroups.com
> I threw your code into a test project and was able to run it without any
> errors using RhinoMocks (trunk) and MBUnit GUI 2.4.197. I assume from your
> description of the problem I should have seen some sort of RhinoMocks
> constraint exception?

This sounds a lot like this problem to me:
http://groups.google.com/group/castle-project-devel/browse_thread/thread/2ab9c89ef08a710f/373037a8293c5069

If it is the same, there's probably nothing Ayende can do to resolve
it. (Of course, I might be wrong, as I haven't analyzed the code
generated by RhinoMocks for this example; but it does sound a lot like
it.)

Fabian

Ayende Rahien

unread,
Jan 22, 2008, 3:42:21 AM1/22/08
to Rhino...@googlegroups.com
Does this happen on the trunk?

jasonm...@gmail.com

unread,
Jan 22, 2008, 1:56:04 PM1/22/08
to Rhino.Mocks
Thank you for your feedback. It seems it's a constrained generics
issue with debugger.
this would explain why it only crashed using mbunit.autorunner and not
Con or GUI. (nUnit Con and GUI also executed without crashing).

Ayende, by 'trunk' you mean svn trunk, correct? I downloaded 3.3 from
your website that's what I was developing with.

Totally unrelated but worth noting. There are no new messages on the
Rhino.Mocks Google Group home page since Jan 14. I'm using IE 7. I
found your responses by
clicking on about this group
archive jan 2008 (40 posts)
sorting outline by date and looking for my post on jan 21.
They messages are also not appearing in my google reader.

On Jan 22, 3:42 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> Does this happen on the trunk?
>
> > Rhino.Mocks 3.3- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
Jan 23, 2008, 6:24:08 AM1/23/08
to Rhino...@googlegroups.com
Not sure about that at all. I'll check with google


Thank you for your feedback.  It seems it's a constrained generics
issue with debugger.
this would explain why it only crashed using mbunit.autorunner and not
Con or GUI. (nUnit Con and GUI also executed without crashing).

Ayende, by 'trunk' you mean svn trunk, correct? I downloaded 3.3 from
your website that's what I was developing with.

Totally unrelated but worth noting. There are no new messages on the
Rhino.Mocks Google Group home page since Jan 14. I'm using IE 7. I
found your responses by
      clicking on about this group
      archive jan 2008 (40 posts)
      sorting outline by date and looking for my post on jan 21.
They messages are also not appearing in my google reader.

jasonm...@gmail.com

unread,
Jan 23, 2008, 9:11:17 AM1/23/08
to Rhino.Mocks
not to hijack the thread, but this was the first response I received
in my google reader. and on the rhino mocks group homepage. must be a
google glitch.

On Jan 23, 6:24 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> Not sure about that at all. I'll check with google
>

Ayende Rahien

unread,
Jan 23, 2008, 9:26:36 AM1/23/08
to Rhino...@googlegroups.com
I think this is because I am the group owner


not to hijack the thread, but this was the first response I received
in my google reader. and on the rhino mocks group homepage. must be a
google glitch.

Reply all
Reply to author
Forward
0 new messages