In many situations, when writing tests, this is either impossible or simply too dangerous to verify that the action actually took place (i.e. a push notification was made). Imagine a unit test entry for the rm command, where you want rm -rf / succeed. Obviously, you cannot allow this action and make sure that your root partition is really empty!
However, you can (really, really do) make sure that all the commands, subroutines, or other actions necessary to complete the task run correctly, without actually allowing them to take place.
In your specific situation, you do not need to verify that your push notification has been delivered, because your application is not responsible for the delivery of notifications. However, you can verify that the push message is delivered to the push server correctly.
So, instead of checking for successful delivery, you will test
- Is the outgoing request formatted correctly (e.g. JSON)
- Will they contain the data that you expect from it (for example, a field in JSON is present and contains the expected data)
- Is the authentication token required by the server enabled
- Is the destination server correct (i.e. you are really sending data to xxx.apple.com and not to localhost).
Ideally, these test requests will not even reach the target server - this means that you rely on two factors that are not always completely stable:
- network connection
- target server availability and proper functionality
In the past, I dealt with this in order to manually issue the correct request first, grab the response, and then ridicule the entire message in unit test (using nock . This way I have full control over the whole message.
source share