Is it possible to mock a type with an attribute?

63 views
Skip to first unread message

mark Kharitonov

unread,
Oct 5, 2011, 5:50:36 AM10/5/11
to rhino...@googlegroups.com

I have this type:

[RequiresAuthentication]
public class MobileRunReportHandler : IMobileRunReportHandler
{
 
public void Post(MobileRunReport report)
 
{
   
...
 
}
}


I am mocking it like so:

var handler = MockRepository.GenerateStub<IMobileRunReportHandler>();
handler
.Stub(x => x.Post(mobileRunReport));


The problem is that the produced mock is not attributed with the RequiresAuthentication attribute. I want the mocked type to be attributed with the RequiresAuthentication attribute, because the code that I am testing makes use of this attribute. I would like to know how can I change my mocking code to instruct the mocking framework to attribute the produced mock accordingly.

Note, that I cannot use IReflectionService, because I have no control over the code which expects the attribute, so the mock must be attributed for real.

Thanks.

P.S.
I have original posted this question on SO - http://stackoverflow.com/questions/7588597/is-it-possible-to-mock-a-type-with-an-attribute-using-rhino-mocks, but no one has provided any answer...

Stephen Bohlen

unread,
Oct 5, 2011, 8:53:52 AM10/5/11
to rhino...@googlegroups.com
This may be one of the cases where hand-rolling your own mock/stub instance is actually a better approach than a framework.

Steve Bohlen
sbo...@gmail.com
http://blog.unhandled-exceptions.com
http://twitter.com/sbohlen


--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhinomocks/-/oY4c0VAIRaIJ.
To post to this group, send email to rhino...@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.

Felix Watts

unread,
Oct 5, 2011, 8:58:00 AM10/5/11
to rhino...@googlegroups.com

If you use moq instead you can simply mock the concrete class with the attribute already applied:

var m = new Mock<MobileRunReportHandler>();

Regards,
Felix

Gavin van der Merwe

unread,
Oct 5, 2011, 10:14:52 AM10/5/11
to rhino...@googlegroups.com
I think what you actually mean felix is using a partial mock which would be 

var m = new Mock<MobileRunReportHandler>() { CallBase = true };

and then you would SetUp the calls you dont want to get through.

The Rhino equivalent would be to use 

MockRepository.GeneratePartialMock<T>() etc.

Also this stub above would not work properly because your Post method is not virtual ... 

Felix Watts

unread,
Oct 5, 2011, 10:30:04 AM10/5/11
to rhino...@googlegroups.com

To clarify, my original post works if you use moq. You don't necessarily need the call base depending on your requirements. Moq allows mocking of non virtual members just there same as virtual ones.

Regards

Gavin van der Merwe

unread,
Oct 5, 2011, 10:32:45 AM10/5/11
to rhino...@googlegroups.com
Yeah, although I think you were on the right track with the partial mock. With any luck the attribute would be inherited although I have not tested this ... :)

Patrick Steele

unread,
Oct 5, 2011, 11:04:07 AM10/5/11
to rhino...@googlegroups.com
You can NOT mock non-virtual methods with moq (unless there's been a
major update to moq that I missed). Moq uses the same DynamicProxy
generator that Rhino.Mocks uses and their approach (generating a
subclass and overriding virtual members to get method interception) is
the same -- and they are therefore both limited to only intercepting
virtual members of a class.

---
Patrick Steele
http://weblogs.asp.net/psteele

Felix Watts

unread,
Oct 5, 2011, 11:25:07 AM10/5/11
to rhino...@googlegroups.com

I stand corrected! I thought I had used that behavior in the past.

Regards

Reply all
Reply to author
Forward
0 new messages