【问题标题】:Codeception Cest MockeryCodeception Cest 嘲弄
【发布时间】: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 或使用{} 工具栏按钮进行格式化。谢谢。

标签: codeception mockery


【解决方案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;
});

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2017-09-01
    • 2019-08-03
    • 1970-01-01
    • 2014-10-27
    • 2015-05-13
    • 2018-12-24
    • 2020-09-06
    • 2017-04-03
    相关资源
    最近更新 更多