Codeception Cest Mockery

时间:2016-12-28 07:21:16

标签: codeception mockery

 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));
 }

我想要运行这个功能测试,但由于嵌入到页面中的重新计算,它失败了。我的嘲笑对象似乎没有任何影响。我是以错误的方式解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

看起来你必须使用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;
});