문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top