#Testing try catch blocks

10 messages · Page 1 of 1 (latest)

gritty canopy
#

Hello everyone.

I have been writing some tests for my application but can't seem to figure out a way of testing try catch blocks, even after doing some research.

In my LoginUserController.php, suppose my code throws an error, which I'm catching using try/catch block, how do I test for this?

In my AuthenticationTest.php, how do I write the tests for test_exceptions_are_caught_during_login method?

red shell
#

You write a test case that should succeed, then you write another test case with bad data that should trigger an exception and add an expectation for that exception.

#

Or, you use Laravel properly and just let the exception handler handle exceptions (like its name suggests) instead of just gobbling up any exception in every controller method you write.

#

You also don’t want to return back on successful login because that will probably just sent you in an infinite redirect loop.

#

Why you need to do manually authentication when Laravel offers at least half a dozen first-party packages for various authentication strategies, I don’t know though.

#

If you’re going to post a message, it’s polite to stay online for more than 5 seconds for any replies, @gritty canopy

gritty canopy
#

@red shell Apologies for going offline immediately after posting, had some errands to run.

I do not have much experience with testing, that's why I raised this issue. I appreciate your feedback and I will look into some suggestions you offered.

gritty canopy
gritty canopy