Laravel $this->withoutExceptionHandling(); Not Working - laravel

I'm running Laravel 5.8 and this function isn't working for my tests. I've put it in the individual tests as the laracast video shows and i've also put it in the setup method. I'm still getting Failed asserting that 500 matches expected 201 type errors without a stacktrace. Anybody know why this could be?
PHPUnit 7.5.20 by Sebastian Bergmann and contributors
/** #test */
public function test_name(){
$this->withoutExceptionHandling();
$response = $this->post("path/url/");
$this->assertEquals(201, $response->getStatusCode());
}

Related

Laravel test without exception, throws exception

I want to test a missing record in the database using Laravel and PHPUnit.
I have setup my tests and set that the method throws an exception. The test gives an error, because the exception has been thrown.
This is my test:
/** #test */
public function myTest()
{
$this->withoutExceptionHandling();
$this->_createMemberRequest(['email_address' => $this->email_address]);
}
The testresults:
There was 1 error:
1) myTest
Exceptions\InvalidEmailException: Email already exists
What am I doing wrong?
Thanks for help!
The whole point of $this->withoutExceptionHandling(); is when you get a funky error and you dont know what to do or the error is not clear enough but if you are trying to test your form validations ('name' => 'required') or something like that when storing a request then you have to remove $this->withoutExceptionHandling(); to get your test passed.

Laravel 5.4 using Mail fake with Mail::queue, Mail::assertSent not working

I am writing unit test for a code that sends email with Mail::queue function, like the one in the documentation: https://laravel.com/docs/5.4/mocking#mail-fake
My Test:
/** #test */
public function send_reminder()
{
Mail::fake();
$response = $this->actingAs($this->existing_account)->json('POST', '/timeline-send-reminder', []);
$response->assertStatus(302);
Mail::assertSent(ClientEmail::class, function ($mail) use ($approver) {
return $mail->approver->id === $approver->id;
});
}
Code being Tested:
Mail::to($email, $name)->queue(new ClientEmail(Auth::user()));
Error Message:
The expected [App\Mail\ClientEmail] mailable was not sent.
Failed asserting that false is true.
The email is sent when I manually test it, but not from Unit Test. I'm thinking it might be because I am using Mail::queue instead of Mail::send function.
In .env file, I have
QUEUE_DRIVER=sync and MAIL_DRIVER=log
How can I test Mail::queue for Laravel?
Found the solution:
The problem was that the class was not imported at the top of PHPUnit test file.
Importing the mail class solved the issue. Strange how it didn't error out the testcase itself.

Laravel integration test throwing spurious 404 error when run as a class, individual tests work fine

I'm using the new integration testing stuff in L5.1. I'm getting some strange errors when running a test class - mostly 404's on the pages I'm testing. Oddly, when I filter down to an individual test the test passes fine. When I run the whole test class or test suite that it's a part of, it fails with a 404. The route works in the browser, and the test passes when I run by itself, so it's clearly not a valid error.
The code looks something like this:
class MyTest extends \TestCase
{
use WithoutMiddleware;
public function __construct() {
parent::__construct();
$this->mock = m::mock('MyApp\Stuff\Repository');
$this->validator = m::mock('Illuminate\Validation\Factory');
$this->mockedClass = m::mock('MyApp\Stuff\Service');
}
/**
* #test
*/
public function it_should_get_all_thingies() {
$this->mockedClass->shouldReceive('someMethod')
->once()
->andReturn('yay');
$this->app->instance('MyApp\Stuff\Service', $this->mockedClass);
$this->visit('/api/v1/thingies');
}
}
When I run
phpunit --filter=it_should_get_all_thingies , it works fine.
When I run
phpunit --filter=MyTest, it dies with a 404. I can copy the relevant URL from the error message into the browser and it works fine.
The only other relevant fact that I can think of is that this on an updade from L4.2 to 5.0 to 5.1.
Any help is greatly appreciated.
Figured out what was triggering the error and how to work around it. I was pulling in a secondary routes file like this:
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'api/v1'], function() {
require_once_app_path(__DIR__ . '/routes2.php');
});
The require_once_app_path triggers the issue. Didn't happen in L4.2 or L5.0, started when we upgraded to 5.1. Replacing that with require seems to sort things out.

Why would the example test in Laravel fail for redirect?

Please explain why Laravel 4's example test fails for me.
My "/" route redirects to "login" in my routes.php file.
(Oh, I've RTFMed and scoured several sites, blogs, and tuts.)
-sh-3.2$ phpunit
PHPUnit 4.0.7 by Sebastian Bergmann.
Configuration read from /home/dev/phpunit.xml
The Xdebug extension is not loaded. No code coverage will be generated.
F
Time: 83 ms, Memory: 13.50Mb
There was 1 failure:
1) ExampleTest::testBasicExample
Failed asserting that false is true.
/home/dev/app/tests/ExampleTest.php:14
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
-sh-3.2$
Here is the code from ExampleTest.php
<?php
class ExampleTest extends TestCase {
/**
* A basic functional test example.
*
* #return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
The test is failing because you're getting 302 status code instead of 200 (since there is a redirection that wasn't in the default routes.php file).
If you want to test redirection, then Laravel provides several methods just for that.
If you're using named routes:
$this->assertRedirectedToRoute('login');
If not:
$this->assertRedirectedTo('login');
For testing response status codes:
$this->assertResponseStatus(302);

Class 'Eloquent' not found when mocking in Laravel

I'm following through Jeffrey Way's Laravel Testing Decoded and I've hit an issue I can't seem to fix.
I'm actually work through this tutorial: http://net.tutsplus.com/tutorials/php/testing-laravel-controllers/ Which is an excerpt from his book.
Basically I have a test like so:
class PostsTest extends TestCase {
public function __construct()
{
$this->mock = Mockery::mock('Eloquent', 'Post');
}
And that like for mocking Eloquent and Post returns:
PHP Fatal error: Class 'Eloquent' not found
When I run phpunit. Incidentally if I use Jeffrey's Laravel Generators and just generate some scaffold e.g.
php artisan generate:scaffold post --fields="title:string, body:string"
And run phpunit I get same error. He's using the same:
$this->mock = Mockery::mock('Eloquent', 'Post');
To mock the classes. Does anyone have any suggestions on what the issue could be?
I've been working through the tutorial again from scratch and am still getting the same error. I've pushed it to a public repo so people can see: https://github.com/RyanHavoc/tdd-laravel
Just pull it down, run composer install/update and phpunit.
I found a solution to the issue.
//Causes the Class 'Eloquent' not found error
public function __construct()
{
$this->mock = Mockery::mock('Eloquent', 'Post');
}
//Setting the mocks in the setUp() method instead works
public function setUp()
{
parent::setUp();
$this->mock = Mockery::mock('Eloquent', 'Post');
}

Resources