3

I'm writing some integration tests for a big Zend Framework app of mine, using Zend_Test. But I'm stuck knowing how to write tests for a few of my controllers that utilize a custom-built web API. I am aware of Zend_Controller_Response_HttpTestCase, which the ZF manual indicates could be helpful here, but I find the documentation to be really sparse.

How can I write my tests without having them call the remote server? Best practices? If Zend_Controller_Response_HttpTestCase is called for, then how can I use it? Here's a typical test method I would want to use this in:

class FooControllerTest extends ControllerTestCase {

    public function testMyNiftyFooPage() {
        $this->dispatch('/foo'); // a page that calls a remote API

        $this->assertQueryContentContains('h1', 'Hello World');
        // other assertions, etc.
    }
}
curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • 1
    What does that remote call do? Is it important that you know what the response is in the test result? – Calvin Froedge Aug 16 '11 at 06:23
  • 1
    See [this question](http://stackoverflow.com/questions/4850452/how-to-unit-test-calls-to-google-api), especially the comment from @Marcin to my answer in which he recommends checking out how the framework's own unit tests handle remote calls. – David Weinraub Aug 16 '11 at 11:02
  • +1 David -- I never thought about looking through ZF's own unit tests. Thanks. – curtisdf Aug 16 '11 at 15:05
  • Calvin -- The remote call pulls in some data from a remote server, which the page needs to render. – curtisdf Aug 16 '11 at 15:06

1 Answers1

0

Encapsulate the logic that handles the remote calls in a class, have your controllers accept an instance of it (this is called dependency injection) and mock it for the tests.

This is how testing is usually done - if you're writing a rocket launcher software you should not be launching a nuclear bomb every time your tests run.

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
  • Thanks Emil. Looks like I'll have to forgo the $this->dispatch() call and manually do the test using Mock objects like you said. – curtisdf Aug 16 '11 at 15:07