Codeception Cest Mockery

407 views Asked by At
 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));
 }

I have this functional test I want to run but, it's failing because of the recapcha embedded into the page. My mocked objects don't seem to have any affect. Am I going about this in the wrong fashion?

1

There are 1 answers

0
Michael Pollind On

Looks like you have to use 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;
});