ludo237
198
1
Testing

Testing events with PHPSpec

Posted 4 years ago by ludo237

Hello everyone.

I'm building a test suite and I'm testing my command RegisterUser that has a method handle() like this

    public function handle(User $user)
    {
        $inputs = (array)$this;
        $user   = $user->createUserAndTrashIt($inputs);
        event(new UserWasRegistered($user, $inputs));

        return $user;
    }

At the moment I create this method for my spec

    function it_handles_user_creation(UserRepository $user)
    {
        $user->createUserAndTrashIt($this->userData)->shouldBeCalled();

        $this->handle($user);
    }

And it works fine but I got 1 Broke message from PHPspec

- it handles user creation
      error: Argument 1 passed to app\Events\UserWasRegistered::__construct() must be an instance of app\Entities\User,
      null given, called in /home/vagrant/code/Web/testing/app/Commands/RegisterUser.php on line 78 and defined in
      /home/vagrant/code/Web/testing/app/Events/UserWasRegistered.php line 27

This is cause by the event calls, also because I've mock the repository I don't receive anything back from it. How can I improve this test in order to include the event ?

I'm watching https://laracasts.com/series/testing-jargon/ but still have no clue how to solve it (I'm episode 4 atm)

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.