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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?