5

I am trying to create a test for my laravel 5.2 project which will test a register page in a rest resource controller. When I test it manually with the exact input of my test everything works but when I test it with phpunit I get redirected to an error page making my page assert fail. The error however displays the error of the assert failing and not what is on the error page and thus doesn't show why the test failed. How can I see this?

Testcase:

class registerTest extends TestCase
{
use WithoutMiddleware;

public function testRegisterCompanyCorrectly()
{
    $this->actAsAdmin();
    $this->visit('/Companies/create')
        ->type('testCompany', 'CName')
        ->type('www.testCompany.com', 'CUrl')
        ->type('testManager@gmail.com', 'Mail')
        ->type('John Doe', 'Name')
        ->type('Keith Richards', 'Password')
        ->type('Keith Richards', 'CPassword')
        ->press('Registreren')
        ->seePageIs('/Companies/3')
        ;
}

private function actAsAdmin()
{
    $user = User::find(1);
    $this->actingAs($user);
}
}'

phpunit error:

There was 1 failure:

1) registerTest::testRegisterCompanyCorrectly
Did not land on expected page [http://localhost/Companies/3].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/Companies/3'
+'http://localhost/Companies'

C:\Users\Stefan\Documents\producten\Applicatie\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:170
C:\Users\Stefan\Documents\producten\Applicatie\tests\registerTest.php:34
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

FAILURES!
Tests: 5, Assertions: 14, Failures: 1.
Stefan
  • 323
  • 2
  • 15
  • It expects to land on /companies/3 and lands on /companies instead. Are you sure that when you register a new company you land on the actual page of the newly created resource? Correct me if I am wrong but it also looks pretty weird to pass a hard-coded number to your seePageIs method since that will probably change with each newly create resource. – Stephan-v Oct 25 '16 at 10:10
  • My resource controller returns you to the creation page if you have a basic registration error (wrong values etc). If it succeeds it sends you to the page of the new company(/Companies/{id}). While making the page however I found that it redirects to /Companies/ with an error message if laravel encountered an error. The problem is, without being able to manually reproduce it or show the error I can't find the problem. Also I currently use database migrations and seeding to prepare the testing but once this works I want to refactor it and make sure there are no hardcoded values. – Stefan Oct 25 '16 at 10:18
  • I'm not very experienced with phpunit but you could try running it with --verbose or --debug to see if you can get some additional information. – Stephan-v Oct 25 '16 at 11:14
  • If your application makes sure that a user can be created only by an admin user, I bet that your migrations and seeds fail to create the user in the testing environment. Try to disable this check temporarily and run the test again. – Armen Markossyan Jun 11 '17 at 09:46
  • The problem here is that your test is *not* a unit test. It's a *functional* test which is supposed to verify that a certain peace of functionality works *in general* but without giving you too much detail. If you had a unit test which verifies that a particular validation class works as expected, you would have had a more detailed information on what is actually wrong. Functional tests are focusing on making sure that you know that there's *something* wrong with a particular functionality (user registration) of your application, so you know where to investiage. – Armen Markossyan Jun 11 '17 at 09:50

1 Answers1

0

One way to investigate the error would be to activate error reporting for Illuminate\Foundation\Validation\ValidationException in your app/Exceptions/Handler.php.

protected $dontReport = [
    // ValidationException::class,
];

If you run the test again, there should be a stack trace in your log file.

Felix
  • 812
  • 5
  • 11