How to get a list of all methods invoked on a mocked object

68 views
Skip to first unread message

Mirko Seifert

unread,
Aug 7, 2012, 5:16:12 AM8/7/12
to rhino...@googlegroups.com
Hi,

is there a way to get a list of all methods that were invoked on a mocked object?

I've tried using GetArgumentsForCallsMadeOn() but this required to specify the methods I'm interested in. Rather, I want all calls.

Thanks in advance,

Mirko

Patrick Steele

unread,
Aug 7, 2012, 9:58:52 AM8/7/12
to rhino...@googlegroups.com
Not that I know of. Why do you want this?

---
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/-/60O6efnZX2cJ.
> 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.

Mirko Seifert

unread,
Aug 7, 2012, 10:37:55 AM8/7/12
to rhino...@googlegroups.com
I want to test a custom UI control, more specifically its invokations on the Graphics object. The are many such invocations and I don't want to specify all of them. Rather, I'd like to iterate over a list of all invokations and check whether my assertions are met.

The invocations are recorded anyway, right? Rhino mock needs them to check the assertion. Can I access the list of recorded invocations or can I add a custom handler that records them? I'm willing to modify the source code of the Rhino Mock library if this is needed.

Cheers,

Mirko



Am Dienstag, 7. August 2012 15:58:52 UTC+2 schrieb Patrick Steele:
Not that I know of.  Why do you want this?

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


On Tue, Aug 7, 2012 at 5:16 AM, Mirko Seifert wrote:
> Hi,
>
> is there a way to get a list of all methods that were invoked on a mocked
> object?
>
> I've tried using GetArgumentsForCallsMadeOn() but this required to specify
> the methods I'm interested in. Rather, I want all calls.
>
> Thanks in advance,
>
> Mirko
>
> --
> 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/-/60O6efnZX2cJ.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
> rhinomocks+unsubscribe@googlegroups.com.

Patrick Steele

unread,
Aug 7, 2012, 12:51:33 PM8/7/12
to rhino...@googlegroups.com
Rhino mocks probably does have some internal list to track the names
of the *virtual* methods it intercepted on the mock objects it
created. But that wouldn't help you with the Graphics class since:

1. You aren't mocking it (or are you wrapping it in an interface?)
2. I checked a half-a-dozen of the methods on the Graphics class and
they aren't virtual -- meaning Rhino.Mocks can't intercept/track their
invocation.

Are you looking to make sure specific graphic calls are made with
specific parameters?

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


>> > rhinomocks+...@googlegroups.com.
>> > 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 view this discussion on the web visit
> https://groups.google.com/d/msg/rhinomocks/-/t9uWkOuOd_cJ.
>
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
> rhinomocks+...@googlegroups.com.

Guillaume

unread,
Aug 7, 2012, 2:03:39 PM8/7/12
to rhino...@googlegroups.com
Hi,

Did you want something like that?

using System;
    using System.Linq;
    using System.Reflection;
    using Rhino.Mocks;
    public static class RhinoMocksCustomExtensions
    {
        public static MethodInfo[] GetInvokedMethods<T>(this T target)
        {
            return typeof(T).GetMethods().Where(m => target.GetArgumentsForCallsMadeOn(
                x => m.Invoke(x,
                    m.GetParameters().Select(p => p.ParameterType.IsValueType ? Activator.CreateInstance(p.ParameterType) : null).ToArray())
                , e => e.IgnoreArguments()).Count != 0).ToArray();
        }
    }

It should give you an array containing every invoked method on the target.

Test :

public interface ITestClass
        {
            void ShouldBeInvoked(string testString, int testInt);
            void ShouldNotBeInvoked();
        }

        /// <summary>
        ///Test pour GetInvokedMethods
        ///</summary>
        [TestMethod()]
        public void GetInvokedMethodsTest()
        {
            ITestClass mock = MockRepository.GenerateMock<ITestClass>();

            mock.ShouldBeInvoked("whatever", 42);

            var invoked = mock.GetInvokedMethods();
            Assert.AreEqual(1, invoked.Count());
            Assert.IsTrue(invoked.First().Name.Equals("ShouldBeInvoked"));
        }

Have a nice day.
Guillaume Loiselle

Mirko Seifert

unread,
Aug 8, 2012, 5:02:47 AM8/8/12
to rhino...@googlegroups.com
Thank you for the answers!

I've tried the GetInvokedMethods() method and after excluding a couple of methods that throw an exception when they are invoked, the code worked. However, the control I'm trying to test (Chart) does not seem to invoke any methods on the Graphics object. I invoked a method (Clear()) to check whether the mocked object is used and this call is indeed returned by GetInvokedMethods(). Maybe Chart uses unmanaged code to draw itself?
Reply all
Reply to author
Forward
0 new messages