使用laravel 开发完api接口后 , 为api编写测试脚本可以大大提高测试效率
phpunit vendor/custom/test/exampleTest.php
laravel 的 session 及cache 是通过数组进行构造的 , 如下
class exampleTest extends testCase{
public function abTest(){
$session = ['key'=>value];
$cache = ['key' => value];
Cache::shouldReceive('get')->with('key')->addReturn($cache); //Cache::get('key') == $cache
Cache::shouldReceive('pull')->with('key')->addReturn(null); //Cache::pull('key') == null
$res = $this->withSession($session)->json('get','http://',$data);
$res->assertStatus(200);
}
}