I’m using phpunit framework and I have a code like this:
public function A() {
try {
(...some code...)
die (json_encode ($data));
}
catch (Exception $e) {
die(false);
}
}
This function is called via AJAX and I can’t replace die with return. The question is:
How I can make a unit test with a code like this?
I can’t use for this, the asserts.
Thanks.
You can’t test that…
Sometimes unit testing brings up problems like this (untestable situations). That usually means that the problem is not with the testing, but with your code and its architecture.
Here you shouldn’t use the
diefunction (actually you shouldn’t usedieto return a HTTP response), butechothe json and then let the script finish properly (orreturnthe json andechoit somewhere else).To test this, you can then capture the output and check it (this is a basic example, there is much better I guess).
Conclusion : the problem is with your code, fix this and then you can try to test it. If you can’t, then no testing.