How do I test If a method was called within another method

94 views
Skip to first unread message

Robert Kuzelj

unread,
Feb 27, 2013, 9:41:41 AM2/27/13
to mock...@googlegroups.com

Hi,

I have this class

class Action {
    public function execute(subject){run(subject);}
}

And the following test

[Test]
public function has_run_been_called() {
    var action:Action = nice(Action);
    var subject = {};
    action.execute(subject);
    assertThat(action, received().method('run').once()); //this fails 
}

Sadly the assert fails and I have no idea how to test this scenario.


Your help is appreciated.

Thanks

Robert

Hob Spillane

unread,
Feb 27, 2013, 1:12:54 PM2/27/13
to mock...@googlegroups.com
A couple of things:

1.  The "subject" of your test here seems to be Action.  That being the case, you wouldn't want to mock it.  You would, instead, create a real instance of Action, and test it works by creating expectations of how it interacts with the objects it depends on.
2.  Rather than trying to verify that some protected method on Action is called, you should be creating expectations on the objects that Action interacts with (in this case your subject var).

Since I don't have the body of your run() method, I'm not sure what your supposed to be expecting here, but a simple example would look something like this:

[Rule]
public var mocks:MockolateRule = new MockolateRule();
    
[Mock(type="nice",inject="true")]
public var subject:Object;

[Test]
public function subjectHadFooCalled_whenExecuteIsCalled() {
    var action:Action = new Action();
    expect(subject.foo());
    action.execute(subject);
}

This assumes that somewhere in your run() method, you're going to call foo() on "subject".


Robert

--
You received this message because you are subscribed to the Google Groups "Mockolate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockolate+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Robert Kuzelj

unread,
Feb 28, 2013, 4:05:11 AM2/28/13
to mock...@googlegroups.com
Hi Hob,

thanks for your answer.

I do understand your point in principle however there is a reason why I want to check for
the "run" method.

In the end what I really do is to test the "execute" method which is a pretty complex
method that does a lot of routing either to internal methods or to other dependent
objects. And I want to see if certain "routes" are being dispatched given a certain input
to "execute".

This works pretty easy and simple for example in JavaScript using Jasmine.

Is there any otherway except for what you proposed?

Thanks

Drew Bourne

unread,
Feb 28, 2013, 6:25:34 PM2/28/13
to mock...@googlegroups.com
Robert, 

Instead of using `nice(Action)` use `partial(Action)`. This will create a mock that will record invocations and call through to the original implementation. You can then use the verification methods to check if they were called. Do note though that only public methods/getters/setters will be recorded. No protected or private methods will be recorded.

cheers, 
Drew
Reply all
Reply to author
Forward
0 new messages