我对AdsController进行了测试。
当我运行$ phpunit
时,我收到消息:
时间:208 ms,内存:16.50Mb OK(3次测试,4次断言)。
然后我移动到文件夹reports/covarage
并打开文件AdsController.php.html
。
并且发现方法getNew()
尚未开始。怎么会?只运行方法index()
。为什么呢?
public function setUp()
{
parent::setUp();
$this->mock = $this->mock('App\Models\Advertisement');
$this->builder = $this->mock('Illuminate\Database\Eloquent\Builder');
}
public function testIndex()
{
$this->mock
->shouldReceive('approved')
->atLeast()->once()->andReturn($this->builder);
$this->builder->shouldReceive('with')->times(4)->andReturn($this->builder);
$this->builder->shouldReceive('orderBy')->once()->andReturn($this->builder);
$this->builder->shouldReceive('paginate')->once()->andReturn($this->builder);
$this->call('GET', 'ads');
$this->assertResponseOk();
$this->assertViewHas('ads');
}
/**
* @return void
*/
public function testGetNew()
{
$this->mock
->shouldReceive('approved')
->atLeast()->once()->andReturn($this->builder);
$this->builder->shouldReceive('with')->times(4)->andReturn($this->builder);
$this->builder->shouldReceive('where')->once()->andReturn($this->builder);
$this->builder->shouldReceive('orderBy')->once()->andReturn($this->builder);
$this->builder->shouldReceive('get')->once()->andReturn($this->builder);
$this->call('GET', 'getNewAds');
$this->assertResponseOk();
}
我在AdsController上的方法:
public function index(Request $request, Advertisement $advertisement)
{
$ads = $advertisement::approved()
->with('city')
->with('ads_attachment')
->with('category')
->with('user')->orderBy('id', 'desc')->paginate(9);
return view('ads.index', ['ads' => $ads]);
//
}
public function getNew(Advertisement $advertisement)
{
$ads = $advertisement::approved()
->with('city')
->with('ads_attachment')
->with('category')
->with('user')->where('id', '>', Input::get('last_id'))->orderBy('id', 'desc')->get();
return \Response::json(['ads' => $ads]);
}
路线:
Route::resource('ads', 'AdsController');//->
$router->get('/getNewAds', 'AdsController@getNew');
路由器在浏览器中工作
UDP
我删除了testIndex()
方法并运行测试。报告查看并看到方法index()
仍在运行。我的记录没有被覆盖?怎么会?访问权限