Question

I'm trying to unit test my code, and I have a datasource that uses the HttpSocket class /lib/Cake/Network/Http/HttpSocket.php.

The problem is that I don't want it to perform requests while testing, but I can't find a way to mock that class.

Of course I could inject it via parameter in the constructor, but I probably would have to change Cake's core, so this isn't an option.

PS: I'm not testing a model that uses this datasource, I'm testing the datasource itself.

Any ideas?

Was it helpful?

Solution

Inside your datasource class have a method that gets you the HttpSocket:

public function getHttpSocket() {
    return new HttpSocket();
}

You can then mock that method of your data source and return a completely mocked HttpSocket object and set your expectations and return values as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top