I'm struggling to make the unit test for my event subscriber work as expected. So here is the test:
$user = new User();
$user->email = self::EMAIL;
$user->subscribed_to_mailing_list = TRUE;
$this->userRepository->expects($this->once())
->method('save')
->with($user);
$event = new UnsubscribedEmailsEvent([self::EMAIL]);//Here i get the webhook from 3-d parties about unsubscription
$this->subscriber->onUnsubscribedListReceived($event);
$this->assertFalse($user->subscribed_to_mailing_list);
and my test fails, because the user still have a true
assigned, but must be the false
.
What I'm doing wrong?
CodePudding user response:
Assertion fails, because user is not returned from repository in your subscriber service. You should inject to your subscriber userRepository mock and then return user from it, for example:
$this->userRepository
->expects($this->once())
->method('getById')
->willReturn($user);