路线存在 - 测试认为它不存在

时间:2016-03-17 16:27:46

标签: laravel laravel-5.2

我的路线在我的“本地”环境中工作正常,但是当我去运行我的单元测试时,它无法说它找不到路线。我已经使用php artisan tinker --env=testing进行了测试,我可以从那里执行路线。

有关为何会发生这种情况的任何想法?

➜  l5_media_communities git:(laravel-5.2-testing) ✗ php artisan tinker --env=testing
Psy Shell v0.7.2 (PHP 5.6.10 — cli) by Justin Hileman
>>> env
Current application environment: testing
>>> route('groups.media.show', [6, 'this-is-a-slug']);
=> "http://localhost/groups/6/media/this-is-a-slug"

这是我运行测试用例时得到的结果。

➜  l5_media_communities git:(laravel-5.2-testing) ✗ phpunit
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.

..E..                                                               5     / 5 (100%)

Time: 2.74 seconds, Memory: 36.00Mb

There was 1 error:

1) UserEmailNotificationsTest::testActiveFollowerReceivesNewCommentEmailNotification
ErrorException: Route [groups.media.show] not defined. (View: /Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/resources/views/emails/notifications/new-comment-owner.blade.php)

/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:307
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:44
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php:59
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:147
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:118
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:83
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/InlineEmail.php:30
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/mailer.php:261
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/mailer.php:203
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/Listeners/NotificationEventListener.php:43
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/tests/UserEmailNotificationsTest.php:132

Caused by
InvalidArgumentException: Route [groups.media.show] not defined.

/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:307
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:616
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/storage/framework/views/d1c9c8d779082ca7537ef8f0e752ed02afc0753e.php:2
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:42
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php:59
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:147
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:118
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/vendor/laravel/framework/src/Illuminate/View/View.php:83
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/InlineEmail.php:30
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/mailer.php:261
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/library/mailer.php:203
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/app/Listeners/NotificationEventListener.php:43
/Users/jcrawford/Dropbox/Work/Viddler/Repositories/l5_media_communities/tests/UserEmailNotificationsTest.php:132

FAILURES!
Tests: 5, Assertions: 4, Errors: 1.

1 个答案:

答案 0 :(得分:1)

使用PHPUnit,路由不能单独定义并包含在routes.php文件中。您可以在RouteServiceProvider.php中添加其他调用,或者为其他路由创建新的服务提供商。

class RouteServiceProvider extends ServiceProvider
{
/**
 * This namespace is applied to the controller routes in your routes file.
 *
 * In addition, it is set as the URL generator's root namespace.
 *
 * @var string
 */
protected $namespace = 'App\Http\Controllers';

/**
 * Define your route model bindings, pattern filters, etc.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function boot(Router $router)
{
    //

    parent::boot($router);
}

/**
 * Define the routes for the application.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
    });

    /* Additional route file */
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/additional_routes.php');
    });
}
}

可能有更好的方法可以做到这一点,但这正是我现在想出来的。