1

我正在使用遵循 MVC 模式的框架编写一个 facebook 应用程序 - Kohna 3.2。我想测试我的服务器端代码。我决定为模型编写单元测试,并为检查控制器/视图编写功能测试。决定两者都使用 PHPUnit。

所以我很快就遇到了问题。如何在请求处理期间为使用外部 api 的控制器编写功能测试?

我不想使用真正的 api 调用。它们的执行需要很长时间,并且需要会话中的身份验证令牌,该令牌很快就会到期。还测试诸如“在墙上写帖子”之类的东西会产生大量垃圾邮件。

我真的看不出如何嘲笑这个。当然,我可以为外部 api 包装库创建一个模拟对象,但我认为应该通过创建请求并检查其响应来进行功能测试。所以我看不到可以注入模拟对象的地方..

你会怎么做?

4

2 回答 2

2

1)当测试与 API 包装器一起工作的东西时,您应该模拟整个 API 包装器类并模拟抛出异常作为错误状态并测试应用程序本身将如何对这些异常做出反应。

它可能应该停止执行一些依赖于 API 响应的操作,并且它可能应该显示一些用户友好的错误。

更重要的是,您可以(并且可能应该)测试 API 包装器上的哪些方法被调用 + 多少次以及传递了哪些参数。

<?php

public function testShowUser() {
    $fb = $this->getMock( 'Facebook\Api' );
    $fb->expects( $this->once() ) // if your library will call getUserInfo() more than once or never, the test will fail
        ->method( 'getUserInfo' )
        ->with( $this->equalTo( 'johndoe' ) ) // if the method will be called with different parameter, the test will fail
        ->will( $this->throwException( 'Facebook\NonExistingUser' ) );

    $myApp = new MyApp( $fb );
    $myApp->renderUser( 'johndoe' ); // if this will throw uncaught exception, the test will fail

    $this->assertEquals(
        array( 'The user you requested does not exist' ),
        $myApp->getFlashMessages()
    );
}

2) 在测试API 包装器本身时,您可以模拟来自 API 的原始响应。

您应该将围绕 HTTP 通信的所有内容分离到某个特定的类(Curl 包装器/带有自己的单元测试/),并假设服务器返回了一些特定的 HTTP 代码和响应。

您可以将所有可能类型的响应保存在文件中,以便将它们作为响应加载到测试中。

我建议这样做:

<?php

/**
 * @expectedException Facebook\NonExistingUser
 */
public function testUnavailableApi() {
    $curl = $this->getMock( 'CurlWrapper' );
    $curl->expects( $this->once() )
        ->method( 'getBody' )
        ->will( $this->returnValue( file_get_contents( 'fb_404_response.json' ) ) );
    $curl->expects( $this->once() )
        ->method( 'getStatusCode' )
        ->will( $this->returnValue( 404 ) );

    $api = new Facebook\Api( $curl );
    $api->getUserInfo( 'johndoe' );
}
于 2012-09-05T15:23:08.017 回答
0

在回顾自己未回答的问题时,我可以回答自己,使用 DIC(依赖注入容器)就是答案。它允许对测试环境进行不同的配置,或者在测试期间使用单个 PHP 进程时,甚至可以在执行测试之前设置模拟。

于 2013-07-02T16:39:12.460 回答