Codeception Cest 嘲弄
Posted
技术标签:
【中文标题】Codeception Cest 嘲弄【英文标题】:Codeception Cest Mockery 【发布时间】:2017-05-12 10:52:32 【问题描述】: public function tryRegister(FunctionalTester $I)
//arrange
$I->disableEvents();
$user = $I->factory()->instance(User::class);
$new_password = str_random(10);
// prevent validation error on captcha
\NoCaptcha::shouldReceive('verifyResponse')
->once()
->andReturn(true);
// provide hidden input for your 'required' validation
\NoCaptcha::shouldReceive('display')
->zeroOrMoreTimes()
->andReturn('<input type="hidden" name="g-recaptcha-response" alue="1" />');
//act
$I->amOnPage(route('auth.register'));
$I->fillField("username",$user->username);
$I->fillField("name",$user->name);
$I->fillField("email",$user->email);
$I->fillField("password",$new_password);
$I->fillField("password_confirmation",$new_password);
$I->click('Register');
$I->seeCurrentRouteIs('auth.login');
//$I->seeEventTriggered(UserRegistered::class);
//assert
$registered_user = \UserRepo::findByField('email',$user->email)->first();
$I->assertFalse(\Auth::check());
$I->assertTrue($user->name == $registered_user->name);
$I->assertTrue($user->username == $registered_user->username);
$I->assertTrue($user->email == $registered_user->email);
$I->assertTrue(\Hash::check($new_password,$registered_user->password));
我想要运行这个功能测试,但是由于页面中嵌入了 recapcha,它失败了。我的模拟对象似乎没有任何影响。我是否以错误的方式处理这件事?
【问题讨论】:
请不要阻止引用代码。只需在代码编辑器中正确格式化(最好使用空格而不是制表符),将其复制并粘贴到您的问题中,然后按 Ctrl+K 或使用
工具栏按钮进行格式化。谢谢。
【参考方案1】:
看来你必须使用 haveBinding
$I->haveBinding('captcha',function()
$no_captcha = \Mockery::mock(Anhskohbo\NoCaptcha\NoCaptcha::class);
// prevent validation error on captcha
$no_captcha->shouldReceive('verifyResponse')
->andReturn(true);
// provide hidden input for your 'required' validation
$no_captcha->shouldReceive('display')
->zeroOrMoreTimes()
->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');
return $no_captcha;
);
【讨论】:
以上是关于Codeception Cest 嘲弄的主要内容,如果未能解决你的问题,请参考以下文章