views:

82

answers:

1

I'm writing some test cases, and I've got a test case that is using Mock objects. I need to check to see if two class methods are called from another class method. Here's what I've done:

First I generated the Mock:

Mock::generate('Parser');

Then, inside my test I called:

$P = new MockParser();

$P->expectOnce('loadUrl', array('http://url'));
$P->expectOnce('parse');

$P->fetchAndParse('http://url');

My implementation code looks like:

public function fetchAndParse($url) {
 $this->loadUrl($url);
 $this->parse();
}

And the loadUrl and parse() methods definately exist. I'm getting two failures on my tests, both telling me "Expected call count for [loadUrl] was [1] got [0]". I've got no idea what's going on - the methods are being called from that function!

Thanks,

Jamie

+3  A: 

While my experience has been with mocking frameworks in the .NET world, I think that what you're trying to do is incorrect.

Any mocking framework when asked to create a mock for a class, generates "stubs" for ALL the methods in that class. This includes the method fetchAndParse. So when you are calling fetchAndParse on your mock object $P, the methods loadUrl and parse are NOT called. What you are really doing is calling the "stubbed" fetchAndParse method.

I'm not really experienced in PHP, so I don't want to try and fix your test. Hopefully someone else can do that.

Praveen Angyan
That's really helpful - it means I have to change quite a bit of code around, but at least I now know what's going on! Thanks!
Jamie Rumbelow