Create mock for an abstract class using internal constructor

449 views
Skip to first unread message

Łukasz Podolak

unread,
Aug 13, 2008, 10:29:24 AM8/13/08
to Rhino.Mocks
Hello,

I have an abstract class with two constructors:

public abstract class MyClass
{

protected internal MyClass() : this(GetConfiguration())
{
....
}


internal MyClass(IConfiguration configuration)
{
....
}
}

I want to mock it, but calling the constructor with one parameter.
But, the code:

var sutMyClass = mocks.Stub<MyClass>(new object[]{new
Configuration()});

throws:

System.MissingMethodException: Constructor on type
'MyClassProxy8e791bf4db1c4a8ab8074abd6c8727a7' not found.

I'm running tests with nunit and Rhino Mocks 3.5. Test assembly is
pointed with "InternalsVisibleTo" from main assembly, so the internal
constructor should be visible, but is not.

Where am I wrong?

thanks,
Łukasz Podolak

Ayende Rahien

unread,
Aug 13, 2008, 10:34:13 AM8/13/08
to Rhino...@googlegroups.com
You need to use InternalsVisibleTo, IIRC.
But probably it will not work because the code assumes that you are trying to call a public ctor.

2008/8/13 Łukasz Podolak <lukasz....@gmail.com>

Łukasz Podolak

unread,
Aug 13, 2008, 10:52:57 AM8/13/08
to Rhino.Mocks
Thanks Ayende for a response,

1.) I mentioned before that I have InternalsVisibleTo attribute
properly set in my assembly under test. And it works, I can access
internal methods and constructors from testing assembly. It's just the
creation of mocks that does not seem to respect it. No matter if I try
MockRepository.GenerateStub<T>, MockRepository.GenerateMock<T> or
their equivalents (Partial, Dynamic, Strict) - It does not see that
internal (but theoretically "visible") constructor.


2). "But probably it will not work because the code assumes that you
are trying
to call a public ctor. "
Yes, indeed. When changing the access modifier to public that works,
but the problem is that I do not want to make it public.

However now I see that I can't do anything about it with Rhino so I
should better change a bit my design to ease testing it.

But maybe is it possible to change RM to support this in future
releases?:)
Thanks for explanation.

Łukasz Podolak

On 13 Sie, 16:34, "Ayende Rahien" <aye...@ayende.com> wrote:
> You need to use InternalsVisibleTo, IIRC.
> But probably it will not work because the code assumes that you are trying
> to call a public ctor.
>
> 2008/8/13 Łukasz Podolak <lukasz.podo...@gmail.com>

Ayende Rahien

unread,
Aug 13, 2008, 11:03:18 AM8/13/08
to Rhino...@googlegroups.com
It is certainly possible to change Rhino Mocks.
A patch would be accepted.

2008/8/13 Łukasz Podolak <lukasz....@gmail.com>

Dennis

unread,
Aug 14, 2008, 3:54:13 AM8/14/08
to Rhino.Mocks
http://www.ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx
you need InternalsVisibleTo to RhinoMocks, not only your own testing
assembly.

Łukasz Podolak

unread,
Aug 19, 2008, 7:42:19 AM8/19/08
to Rhino.Mocks
I missed that before!
Thank you for reminding me of that, now it works like a charm.
Reply all
Reply to author
Forward
0 new messages