【问题标题】:how to write a phptest for controller in fuelphp如何在fuelphp中为控制器编写一个phptest
【发布时间】:2015-07-27 08:04:52
【问题描述】:

我只有一个控制器和视图,我想mock一个http请求来测试这个控制器,我用的是fuelphp,希望有人能给我一些建议或demo

class Controller_Index extends Controller_Template{
    public function action_index(){
        $view = View::forge('index');
        $this->template->content = $view;
    }
}

我是这样写的

class Test_Controller_index extends TestCase{
    public function TestController(){
        $expected = View::forge('index');
        $response = Request::forge('index')
                         ->set_method('GET')
                         ->execute()
                         ->response();
        $assertValue = $response->body->content;
        $this->assertSame($expected, $assertValue);
    }
}

php试油结果

There was 1 failure:
1) Warning
No tests found in class "Test_Controller_index".

怎么了

【问题讨论】:

  • 麻烦我了,请问可以请教吗?
  • 如果您阅读错误消息,您会看到测试框架没有接收任何测试。尝试调用你的测试方法testController(),看看它是否可以运行。
  • 另外我注意到你只是在测试视图加载。这种测试是没有用的,因为您所做的只是断言框架可以加载视图,而框架自己的测试已经涵盖了这一点。
  • 感谢您回答我,实际上,我只是想模拟一个 http 请求,因为我的项目已损坏,我没有 db 并且一些 url 跳转未使用,所以我想做这个 mock.er ..现在我不知道这是否正确,
  • 您的测试可能无法运行,因为您需要将方法命名为 testController 而不是 TestController

标签: php phpunit fuelphp


【解决方案1】:

fuelphp中的所有单元测试都需要采用test_的形式,否则无法识别。

试试这个:(不是不同的函数名)

class Test_Controller_index extends TestCase{
    public function test_controller(){
        $expected = View::forge('index');
        $response = Request::forge('index')
                         ->set_method('GET')
                         ->execute()
                         ->response();
        $assertValue = $response->body->content;
        $this->assertSame($expected, $assertValue);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多