Page 1 of 1

Simpletest mocks expect() question

Posted: Thu Jun 21, 2007 7:20 am
by georgeoc
Hi.

Is there a way that a mock can just check for *some* of the parameters passed to a particular method, rather than all of them? I have mocked a database connection, and I'm using it in my language file. When I run:

Code: Select all

$lang->install();
it will pass the language strings to the db like this:

Code: Select all

$this->_db->insert('lang_strings', array(/*very large array of strings*/));
In my test, I'd like to be able to use this:

Code: Select all

$mock_db->expect('insert', array('lang_strings'));
... or similar, and have the test pass. I understand that this is likely to break other uses of the expect() function, so I'm not sure what to do.

All I want my mock to check for is that the database table name ('lang_strings') is correct - testing each element of the large array seems pointless and will create dependency in the test. If it's not possible for the Mock to only take notice of the first parameter, how do you suggest I test the lang class sufficiently?

Many thanks!

Posted: Thu Jun 21, 2007 8:01 am
by Ambush Commander
Yes you can do it, you use the Expectation classes.

Code: Select all

$mock_db->expect('insert', array('lang_strings', new IsAExpectation('array')));
Note that you may also want to consider mocking the language strings source so you don't have to pass that many things.

Posted: Thu Jun 21, 2007 8:14 am
by georgeoc
Great - that's exactly what I was looking for.

Thanks!