如何为功能"beforeControllerAction"创建单元测试?从yii框架扩展 [英] How to create unit test for the function "beforeControllerAction" extended from yii framework

查看:86
本文介绍了如何为功能"beforeControllerAction"创建单元测试?从yii框架扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些想法,以便为yii框架扩展的动作"beforeControllerAction"创建单元测试.

I need some idea, to create unit test for the action 'beforeControllerAction', which is extended from yii framework.

推荐答案

beforeControllerAction是来自框架核心的任何"mycontroller"应用控制器的父方法.您不需要测试特定的核心框架代码(已测试).您需要测试自己的代码.

beforeControllerAction is parent method from any 'mycontroller' app controller, coming from framework core. You don't need to test specific core framework code (is already tested). You need to test your own code.

测试控制器的一种方法是先扩展/继承自己的"mycontroller"控制器,然后为其构建测试.取自此

One way to test your controller is to extend/inherit your own 'mycontroller' controller first and build a test for it. Taken from this excellent article:

在protected/tests/unit下创建您的单元测试类 文件夹,并将其命名为您要测试的班级名称, 在其后添加Test字.

Create your unit test class under the protected/tests/unit folder and name it the same as your class name you want to test, adding a Test word after it.

就我而言,我将创建一个名为ApiControllerTest.php的文件,该文件 包含ApiController.php类的所有测试.

In my case, I will create a file named ApiControllerTest.php that contains all tests for ApiController.php class.

<?php

// You can use Yii import or PHP require_once to refer your original file
Yii::import('application.controllers.ApiController');

class ApiControllerTest extends ApiController 
{ 
}

在步骤1中打开您的ApiControllerTest.php单元测试类 并使其类似于以下内容(根据您的 需求和结构):

Open your ApiControllerTest.php unit test class in step #1 above and make it something similar like this (based on your requirement and structure):

class ApiControllerTest extends CTestCase 
{ 
  public function setUp() 
  {
    $this->api = new ApiController(rand()); 
  } 

  public function tearDown() 
  {
    unset($this->api); 
  } 
}

让我们尝试在ApiController.php中测试一种方法,即 formatResponseHeader.这就是它的作用.

Let’s try to test one single method in my ApiController.php, that is formatResponseHeader. This is what it is doing.

public function formatResponseHeader($code)
{ 
  if (!array_key_exists($code, $this->response_code)) 
  { 
    $code = '400'; 
  } 
  return 'HTTP/1.1 ' . $code . ' ' . $this->response_code[$code]; 
}

现在,要测试此方法,我将打开ApiControllerTest.php并将其添加 在setUp()之后和tearDown()方法之前的以下代码:

Now, to test this method, I’ll open ApiControllerTest.php and add this code below after setUp() and before tearDown() methods:

public function testFormatResponseHeader() 
{
  $this->assertEquals('HTTP/1.1 400 Bad Request',$this->api->formatResponseHeader('400'));
  $this->assertEquals('HTTP/1.1 200 OK',$this->api->formatResponseHeader('200'));
  $this->assertEquals('HTTP/1.1 400 Bad Request',$this->api->formatResponseHeader('500'));
  $this->assertNotEquals('HTTP/1.1 304 Not Modified',$this->api->formatResponseHeader('204'));
}

将更改保存在ApiControllerTest.php中,然后尝试在其中运行 保护/测试目录:

Save the change in ApiControllerTest.php and then try to run this in protected/tests directory:

phpunit .

这篇关于如何为功能"beforeControllerAction"创建单元测试?从yii框架扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆