Mocking a call by something defined in the method to test

40 views
Skip to first unread message

Adam

unread,
Jun 19, 2012, 1:12:46 PM6/19/12
to rhino...@googlegroups.com
I'm new to rhino mocks, so please bear with me, but this is my method:

 public void Foo(CustomClass1 Param1)
        {
            CustomClass2 foo = new CustomClass2(Private void method, Param1)
                { Property = _property };
            foo.Start();
        }

And I want to assert that foo.Start() was called...

[TestMethod()]
        public void FooTest()
        {
            var target = MockRepository.GenerateMock<CustomClass_Accessor>();
            var Param1 = MockRepository.GenerateMock<CustomClass1>();
            target.Foo( Param1 );      
        }

That's where I am now, but I don't see how to access the constructed foo or its methods.

Thanks a lot!

Adam

Patrick Steele

unread,
Jun 19, 2012, 10:52:45 PM6/19/12
to rhino...@googlegroups.com
You can't access foo and Rhino.Mocks won't help you since it needs to
be in control of creating the CustomerClass2 if you want it to
track/stub virtual method calls. Since you have the creation
hard-coded, there's no easy way to test the code as it is currently
written.

One alternative might be to create a factory to control creation of
CustomClass2. During production, the factory would just do as you
currently do -- new up a new CustomClass2. During testing, you could
provide a mock factory which, in turn, could be set up to return a
mock CustomClass2. If "Start" is a virtual method on CustomClass2,
then Rhino.Mocks can intercept and track if it was called.

---
Patrick Steele
http://weblogs.asp.net/psteele
> --
> 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/-/vlc9lvATtyoJ.
> 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.

Adam Van Aken

unread,
Jun 20, 2012, 7:41:29 AM6/20/12
to rhino...@googlegroups.com
Ok, I think I get what you're saying. Thanks very much for the help! I will report any results

Adam

unread,
Jun 25, 2012, 2:36:26 PM6/25/12
to rhino...@googlegroups.com
Yes - I got it all working. Thanks so much for the help, guys!
Reply all
Reply to author
Forward
0 new messages