think-testing icon indicating copy to clipboard operation
think-testing copied to clipboard

对HTTP状态码断言异常

Open Sherman-Chen opened this issue 8 years ago • 0 comments

控制器如下:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function index(){
        return 'test';
    }

    public function index2(){
        $this->redirect('http://www.baidu.com',302);
    }
}

测试类如下:

<?php
namespace tests;
//针对Index控制器
class IndexTest extends TestCase
{
    public function testOutput(){
        $this->visit('/index/index/index')->assertResponseOk();
    }
    public function testNo(){
        $response=$this->visit('/index/index/index2');
        $response->assertResponseStatus(200);//不是302
        $response->see('test');//OK
    }
}

$this->clearInputs()->followRedirects()->assertPageLoaded($uri); 因为这里对于302的状态码都会再次解析,因此会导致对于外域的地址,比如跳转到百度,会造成定位到index控制器下的index方法。

Sherman-Chen avatar Oct 16 '16 05:10 Sherman-Chen