Discussion:
Create mock for an abstract class using internal constructor
Łukasz Podolak
2008-08-13 14:29:24 UTC
Permalink
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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---
Ayende Rahien
2008-08-13 14:34:13 UTC
Permalink
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.
Post by Łukasz Podolak
Hello,
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.
var sutMyClass = mocks.Stub<MyClass>(new object[]{new
Configuration()});
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---
Łukasz Podolak
2008-08-13 14:52:57 UTC
Permalink
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
Post by Ayende Rahien
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.
Post by Łukasz Podolak
Hello,
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.
var sutMyClass = mocks.Stub<MyClass>(new object[]{new
Configuration()});
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---
Ayende Rahien
2008-08-13 15:03:18 UTC
Permalink
It is certainly possible to change Rhino Mocks.
A patch would be accepted.
Post by Łukasz Podolak
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
Post by Ayende Rahien
You need to use InternalsVisibleTo, IIRC.
But probably it will not work because the code assumes that you are
trying
Post by Ayende Rahien
to call a public ctor.
Post by Łukasz Podolak
Hello,
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.
var sutMyClass = mocks.Stub<MyClass>(new object[]{new
Configuration()});
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---
Dennis
2008-08-14 07:54:13 UTC
Permalink
http://www.ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx
you need InternalsVisibleTo to RhinoMocks, not only your own testing
assembly.
Post by Łukasz Podolak
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
Post by Ayende Rahien
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.
Post by Łukasz Podolak
Hello,
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.
var sutMyClass = mocks.Stub<MyClass>(new object[]{new
Configuration()});
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---
Łukasz Podolak
2008-08-19 11:42:19 UTC
Permalink
I missed that before!
Thank you for reminding me of that, now it works like a charm.
Post by Dennis
http://www.ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx
you need InternalsVisibleTo  to RhinoMocks, not only your own testing
assembly.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to RhinoMocks+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...