1

I'm trying to test some of the actions on my controller in Laravel 4 with PHPUnit.

public function testAboutPage()
{
    $this->client->request('GET', 'about');

    $this->assertResponseOk();
}

Also, I bind a variable to my views in the App::before() filter.

App::before(function($request)
{
    View::share('usersCount', User::remember(60)->count());
}

Here is my controller:

class PagesController extends BaseController
{
    public function about()
    {
        return View::make('pages.about')->withTitle("About");
    }
}

However, my tests fail before the variable is not defined in testing. Here is the failing output from PHPUnit.

12) PagesControllerTest::testAboutPage
ErrorException: Undefined variable: usersCount (View: app/views/layouts/application.blade.php) (View: app/views/layouts/application.blade.php)

app/storage/views/ed261635437f52c903a09b2774ce5a92:20
vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:41
vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php:56
vendor/laravel/framework/src/Illuminate/View/View.php:134
vendor/laravel/framework/src/Illuminate/View/View.php:102
vendor/laravel/framework/src/Illuminate/View/View.php:76
app/storage/views/5993084368b648c9a244ec6637315755:18
vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:37
vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php:56
vendor/laravel/framework/src/Illuminate/View/View.php:134
vendor/laravel/framework/src/Illuminate/View/View.php:102
vendor/laravel/framework/src/Illuminate/View/View.php:76
vendor/laravel/framework/src/Illuminate/Http/Response.php:70
vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php:202
vendor/laravel/framework/src/Illuminate/Routing/Router.php:1413
vendor/laravel/framework/src/Illuminate/Routing/Router.php:1003
vendor/laravel/framework/src/Illuminate/Routing/Router.php:968
vendor/laravel/framework/src/Illuminate/Foundation/Application.php:738
vendor/laravel/framework/src/Illuminate/Foundation/Application.php:708
vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:319
app/tests/controllers/PagesControllerTest.php:109

What can I do to resolve this issue? The variable is fine when viewing in a browser or in production.

8
  • Can you post your controller code? Commented Feb 7, 2014 at 4:04
  • Added my controller code. As you can see, it's pretty simple. Commented Feb 7, 2014 at 4:16
  • Your error is in the testUnsubscribedPage method, but you only posted the code for the testAboutPage method. Commented Feb 7, 2014 at 4:18
  • Apologies, this issue affects all my tests on the PagesController. I switched my example from testUnsubscribedPage to testAboutPage as the controller action for it is dead simple. Commented Feb 7, 2014 at 4:22
  • Something's amiss. Your views shouldn't be called at all. Commented Feb 7, 2014 at 4:23

1 Answer 1

2

From the docs, it turns out that filters are disabled. I thought this just meant filters defined in the routes file, but it impacts app before/after filters also.

Note: Route filters are disabled when in the testing environment. To enable them, add Route::enableFilters() to your test.

I've added this to my tests to make them go green.

public function setUp()
{
    parent::setUp();

    Route::enableFilters();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.