As I understand you are currently not writing unit tests, but integration tests (the kind of tests that visit one of your routes and then asserts that everything is OK). These kind of tests are good if you want to know if everything is working, but it is hard to test if specific things are happening (in this case: testing if a specific event was fired).
Instead you should try to write some unit tests: tests that only test 1 specific thing. So instead of asserting that the event was fired when you post to some register route, you assert that the method is made when you register a user through some specific method.
Here is an example:
<?php
class User {
private $email;
private $password;
private $pendingEvents;
public static function register($email, $password)
{
$this->email = $email;
$this->password = $password;
$this->pendingEvents[] = new UserCreated($this);
}
public function releaseEvents()
{
$events = $this->pendingEvents;
$this->pendingEvents = [];
return $events;
}
public function email()
{
return $this->email;
}
public function password()
{
return $this->password;
}
}
class UserTest {
/**
* @test
*/
public function a_user_can_be_registered()
{
$password = 'asdf';
$user = User::register('lorem@ipsum.com', $password);
// check if the user's properties are correctly set
$this->assertEquals($user->email(), 'lorem@ipsum.com');
$this->assertEquals($user->password(), 'asdf');
// check if the appropriate evetns were made
$expected = new UserCreated($user);
$this->assertEquals($user->releaseEvents(), [$expected]);
}
}
You could also do the following:
class User {
private $email;
private $password;
public static function register($email, $password)
{
$this->email = $email;
$this->password = $password;
Event::fire(new UserCreated($email, $password));
}
}
class UserTest {
/**
* @test
*/
public function a_user_can_be_registered()
{
Event::shouldReceive('fire')
->once()
->with([new UserWasCreated('asdf', 'lorem@ipsum.com')]);
$password = 'asdf';
$user = User::register('lorem@ipsum.com', $password);
// check if the user's properties are correctly set
$this->assertEquals($user->email(), 'lorem@ipsum.com');
$this->assertEquals($user->password(), 'asdf');
}
}
But this is a bit harder to test as we have to know the arguments given to the Event::fire() method before we actually create the User object, hence I changed the arguments of the event to take an email and a password.
If anything wasn't clear don't hesitate to ask! Also if you show us some of your (user registration) code I could probably point out what stuff you can change to improve the testability of your code.