mocking Functions

7 views
Skip to first unread message

grep

unread,
Aug 4, 2010, 11:16:11 AM8/4/10
to mockito-flex
Hi there I am using parsley in my project. Parsley provide a messaging
mechanism via a function Object. Here is a example:

package
{
public class SomeClass{

[MessageDispatcher]
public var sendMessage:Function;

public function sendSomeMsg():void
{

}

}
}

grep

unread,
Aug 4, 2010, 11:18:14 AM8/4/10
to mockito-flex
if (a){
sendMessage(new AMessage());
}else{
sendMessage(new BMessage());
}
}

}
}


Is there a way to 'spy' on the Function object so i can test that the
correct message is sent?

Thank a million.
Charlie

Kris

unread,
Aug 4, 2010, 11:40:35 AM8/4/10
to mockit...@googlegroups.com
I would do:

1) Create a class with some function that accepts message.
public class MyMockieSender
{
    public function sendMessage(msg:*):*
    {
    }
}

2) Create mock for this class

var mockieSender:MyMockieSender = mock(MyMockieSender);

3) Take the sendMessage function pointer of the mockieSender and assign it to the SomeClass instance:

var someClass:SomeClass = new SomeClass();
someClass.sendMessage = mockieSender.sendMessage;

4) Make the test call

someClass.sendSomeMsg();

5) And verify on the mock what got called

verify().that(mockieSender.sendMessage(any()));

Hope this helps

Cheers,
Kris


--
You received this message because you are subscribed to the Google Groups "mockito-flex" group.
To post to this group, send email to mockit...@googlegroups.com.
To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito-flex?hl=en.


Reply all
Reply to author
Forward
0 new messages