测试使用 Twitter API 的 Zend_Controller

发布于 2024-10-10 04:08:30 字数 645 浏览 0 评论 0原文

我正在尝试为我的控制器编写一个单元测试,它使用 Zend_Service 调用 Twitter API。

/**
 * Authenticate Step 1 for Twitter
 */
public function authenticateAction()
{
    $this->service->authenticate();
}

该服务确实:

/**
 * Authenticate with twitter
 *
 * @return void
 */
public function authenticate()
{
    $consumer = new Zend_Oauth_Consumer($this->config);
    $token = $consumer->getRequestToken();
    $this->session->twitterRequestToken = serialize($token);
    $consumer->redirect();
    exit;
}

我的问题是我不知道如何替换服务内的身份验证操作以进行单元测试。我不想在测试运行时调用 Twitter API。

有没有可以做这样的事情的模拟框架?

I'm trying to write a unit test for my Controller which calls the Twitter API using Zend_Service.

/**
 * Authenticate Step 1 for Twitter
 */
public function authenticateAction()
{
    $this->service->authenticate();
}

The Service does:

/**
 * Authenticate with twitter
 *
 * @return void
 */
public function authenticate()
{
    $consumer = new Zend_Oauth_Consumer($this->config);
    $token = $consumer->getRequestToken();
    $this->session->twitterRequestToken = serialize($token);
    $consumer->redirect();
    exit;
}

My Problem is that I have no idea how to replace the authenticate action inside the service for the unit test. I don't want to call the Twitter API while the tests run.

Is there any Mocking Framework which can do such things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-10-17 04:08:30

Zend_Service_Twitter 使用 Zend_Http_Client 所以你可以在你的测试用例中存根它

$httpClient = $this->getMock('Zend_Http_Client');

$httpResponse = new Zend_Http_Response(200, array(), $data);  // $data - response you expected to get

$httpClient->expects($this->any())
                   ->method('request')
                   ->will($this->returnValue($httpResponse));

Zend_Service_Twitter::setLocalHttpClient($httpClient);

Zend_Service_Twitter uses Zend_Http_Client so you can stub it in your test case

$httpClient = $this->getMock('Zend_Http_Client');

$httpResponse = new Zend_Http_Response(200, array(), $data);  // $data - response you expected to get

$httpClient->expects($this->any())
                   ->method('request')
                   ->will($this->returnValue($httpResponse));

Zend_Service_Twitter::setLocalHttpClient($httpClient);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文