dongpan8928 2013-09-12 10:57
浏览 44
已采纳

Zend Framework 2:在控制器上进行多少单元测试?

I'm just starting to look into unit testing Zend 2 applications, so forgive me if this is a stupid question.

Let's say I have a controller which updates a model with an array of data, and returns some JSON:

public function testAction($data){
  $model = new \my
amespace\model();
  $model->updateFromArray($data);

  return new JsonModel(array(
        'success' => true,
  ));
}

In this instance, pretty much all of the actual work is done inside the model, in the updateFromArray() method. All the controller does is call this method. When writing unit tests for this controller, I basically have a couple of tests which test that the 'testAction' can be accessed through a particular URL, and that the action returns an instance of JsonModel.

Now I am aware that I need to also unit test the model's 'updateFromArray' method a lot, since it contains all sorts of validation rules etc.

My question is, do I apply these tests to the controller (i.e. write a test that gets to the 'testAction' and send it lots of different arrays of data) or do I directly test the model by just submitting lots of different arrays directly to the updateFromArray method? Or both?

I hope this makes sense!

Update

Just to clarify, should I test like this:

//send lots of different requests to the controller and see if it works
class myControllerTest extends \PHPUnit_Framework_TestCase{

  //some code...//  

  public function testWhatHappensWhenSubmitX(){
    $this->request->getPost()->set('somevariable','x');
    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse();
    $this->assertSomething();   
  }

  public function testWhatHappensWhenSubmitY(){
    $this->request->getPost()->set('somevariable','y');
    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse();     
    $this->assertSomething();     
  }

  public function testWhatHappensWhenSubmitZ(){
    $this->request->getPost()->set('somevariable','z');
    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse(); 
    $this->assertSomething();         
  }
}

Or this:

//have lots of tests against the model
class myModelTest extends \PHPUnit_Framework_TestCase{
  public function setUp(){
    $this->model = new \my
amespace\model()
  }

  public function testWhatHappensWhenSubmitX(){
    $this->$model->doSomething('x');
    $this->assertSomething();   
  }

  public function testWhatHappensWhenSubmitY(){
    $this->$model->doSomething('y');
    $this->assertSomething();     
  }

  public function testWhatHappensWhenSubmitZ(){
    $this->$model->doSomething('z');
    $this->assertSomething();         
  }
}

Or, should I do both?

  • 写回答

2条回答 默认 最新

  • dongtun2459 2013-09-12 18:49
    关注

    We do both, but to different extents.

    Your first style, we only use for a select few system-critical features, as a sort of "smoke-test" to make sure everything runs together. It's important to note that these are not considered true unit tests, but more "functional" tests as the result includes the interaction of your controller with any other classes it uses/interacts with.

    Your second style is what is most common when we test our controllers.

    You should check out the mocking framework, "Phake" http://phake.digitalsandwich.com/docs/html/

    This will help you isolate your controller's functionality and test it properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历