【发布时间】:2016-10-01 02:03:52
【问题描述】:
我正在尝试对一些现有代码进行一些单元测试。我的控制器看起来像
class DefaultController extends Controller
{
public function index() {
if (!Session::get('answers', [])) {
App::abort(403, 'Error.');
}
// Do rest of the stuff here
}
}
我的测试类看起来像
class DefaultController extends extends TestCase {
public function testIndex_withoutSession() {
// Arrange
/* Nothing to arrange now */
// Act
$this->action('GET', 'DefaultController@index');
// Assert
$this->assertResponseStatus(403);
}
public function testIndex_withSession() {
// Arrange
$this->session(['answers' => array()]);
// Act
$this->action('GET', 'ParticipantController@create');
$this->assertSessionHas('answers');
// this function is giving true
// Assert
$this->assertResponseStatus(200);
$this->flushSession();
}
}
我没有会话的测试用例工作正常,但是当我想通过模拟会话变量“答案”来检查它时,它仍然给我错误。任何人都可以通过弄清楚我做错了什么或如何正确做来帮助我吗?没有这个,我无法进一步检查代码。 提前致谢。
【问题讨论】:
标签: unit-testing laravel laravel-5 phpunit