Romain's avatar
Level 30

Method only firing once

Hi there,

I've follow the 'Incremental APIs' series on the site, and upon the test I have a little problem.

I've added a User's model, the model require a unique email, so is set the db.

Now in my test, I try using the times() method that is on the ApiTester class. It just sets a protected variable with the value is it passed. Then I try to create N users using that times method and a getUserStub method that looks like this:

protected function getStubUser($fields = [ ])
 {
    return array_merge( [
    'username' => $this->fake->name,
    'password' => Hash::make( $this->fake->word ),
    'email'    => $this->fake->email,
    'level'    => $this->fake->numberBetween( 1, 5 )
    ], $fields );
 }

And is called like so:

$this->times(2)->make('User', $this->getStubUser());

And the make method:

protected function make($entity, array $stub)
 {
    while( $this->times -- ) {
   $entity::create( $stub );
    }
 }

Now I get a constraint violation from SQlite, saying that email should be unique. I've checked and it seems the stub sent is the same each time. How can I get the stub to be a new one on each pass?

Thanks for any help

0 likes
0 replies

Please or to participate in this conversation.