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 On Wed, Oct 5, 2011 at 10:30 AM, Felix Watts <[email protected]> wrote: > 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 > > On 5 Oct 2011 15:14, "Gavin van der Merwe" <[email protected]> wrote: >> 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 ... >> >> On 5 October 2011 13:58, Felix Watts <[email protected]> wrote: >> >>> If you use moq instead you can simply mock the concrete class with the >>> attribute already applied: >>> >>> var m = new Mock<MobileRunReportHandler>(); >>> >>> Regards, >>> Felix >>> On 5 Oct 2011 10:53, "mark Kharitonov" <[email protected]> wrote: >>> > >>> > >>> > 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... >>> > >>> > -- >>> > 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 [email protected]. >>> > To unsubscribe from this group, send email to >>> [email protected]. >>> > For more options, visit this group at >>> http://groups.google.com/group/rhinomocks?hl=en. >>> > >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Rhino.Mocks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/rhinomocks?hl=en. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Rhino.Mocks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/rhinomocks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Rhino.Mocks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rhinomocks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.
