Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

How to test FCM::sendTo with Feature test #209

Open
javaherisaber opened this issue Feb 9, 2021 · 4 comments
Open

How to test FCM::sendTo with Feature test #209

javaherisaber opened this issue Feb 9, 2021 · 4 comments

Comments

@javaherisaber
Copy link

I have a project integrating Laravel-FCM to send push
I've created a helper class to work with FCM::sendTo and i want to test this class
I use snippets for testing from your README.md file

$numberSucess = 2;
$mockResponse = new \LaravelFCM\Mocks\MockDownstreamResponse(numberSucess);

$mockResponse->addTokenToDelete('token_to_delete');
$mockResponse->addTokenToModify('token_to_modify', 'token_modified');
$mockResponse->setMissingToken(true);

$sender = Mockery::mock(\LaravelFCM\Sender\FCMSender::class);
$sender->shouldReceive('sendTo')->once()->andReturn($mockResponse);

$this->app->singleton('fcm.sender', function($app) use($sender) {
	return $sender;
});

Running this in Test case return following error:

Mockery\Exception\InvalidCountException : Method sendTo(<Any Arguments>) from Mockery_2_LaravelFCM_Sender_FCMSender should be called
 exactly 1 times but called 0 times.
@saber13812002
Copy link

saber13812002 commented May 15, 2021

how can you solve this problem @javaherisaber

@javaherisaber
Copy link
Author

javaherisaber commented May 15, 2021

@saber13812002
As the error stated LaravelFCM_Sender_FCMSender should be called exactly 1 times but called 0 times
You should call $sender->send('args here') in singleton callback

@javaherisaber
Copy link
Author

@saber13812002
I'm still waiting for a proper answer from repository owner

@quyle92
Copy link

quyle92 commented May 4, 2022

@javaherisaber , @saber13812002
The error you are getting is because $sender is expecting the call to happen after you call shouldRecieve(), but it's not - the test case ends, and the Mockery instance has never been called.
Here is what you should do:

$numberSucess = 2;
$mockResponse = new \LaravelFCM\Mocks\MockDownstreamResponse(numberSucess);

$mockResponse->addTokenToDelete('token_to_delete');
$mockResponse->addTokenToModify('token_to_modify', 'token_modified');
$mockResponse->setMissingToken(true);

$sender = Mockery::mock(\LaravelFCM\Sender\FCMSender::class);
$sender->shouldReceive('sendTo')->once()->andReturn($mockResponse);

$this->app->singleton('fcm.sender', function($app) use($sender) {
	return $sender;
});
$response = FCM::sendTo(); //this is equivalent to app('fcm.sender')->sendTo() as FCM is a facade of app('fcm.sender');
$this->assertEquals('token_to_delete',$response->tokensToDelete[0]);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants