laravel 验收测试方法访问未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42645066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
acceptance test method visit undefined
提问by JaChNo
I have created a new laravel 5.4 project with authentication.
我创建了一个带有身份验证的新 Laravel 5.4 项目。
I have then created a feature test for the login process which is as follows
然后我为登录过程创建了一个功能测试,如下所示
public function test_login()
{
$user = factory(\App\User::class)->make();
$user->save();
$this->visit('/login')
->type($user->email,'email')
->type($user->password,'password')
->press('login');;
$this->seePageIs('/dashboard');
}
but when i run the test I get
但是当我运行测试时我得到
Error: Call to undefined method Tests\Feature\UserTest::visit()
错误:调用未定义的方法 Tests\Feature\UserTest::visit()
not sure if I need to install another component. this is what my composer.json looks like
不确定我是否需要安装另一个组件。这就是我的 composer.json 的样子
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/dusk": "^1.0",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\Foundation\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\Foundation\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
回答by Marcin Nabia?ek
There is no visit
method built-in by default in Laravel 5.4 You need to run get
in order to run action but please be aware it is not working the same (probably no redirections are followed).
visit
Laravel 5.4 中默认没有内置方法您需要运行get
才能运行操作,但请注意它的工作方式不一样(可能没有遵循重定向)。
If you want to use old behaviour you can use for now Laravel browser kit;
如果你想使用旧的行为,你现在可以使用 Laravel 浏览器工具包;
composer require laravel/browser-kit-testing
You can read more about it in migration guide: https://laravel.com/docs/5.4/upgrade#upgrade-5.4.0
您可以在迁移指南中阅读更多相关信息:https: //laravel.com/docs/5.4/upgrade#upgrade-5.4.0
As another alternative you can use Laravel Duskto run full browser tests
作为另一种选择,您可以使用Laravel Dusk运行完整的浏览器测试
回答by Suraj
Option 1
选项1
The visit
and see
method no longer works in Laravel 5.4 by default. You need to install Laravel Dusk package.
默认情况下,visit
andsee
方法不再适用于 Laravel 5.4。你需要安装 Laravel Dusk 包。
composer require --dev laravel/dusk
php artisan dusk:install
So you can use visit
and assertSee
method respectively.
所以你可以分别使用visit
和assertSee
方法。
For more info refer to laravel documentation Laravel Dusk Documentation
有关更多信息,请参阅 Laravel 文档Laravel Dusk 文档
Option 2
选项 2
Sometimes you already have written test cases and you don't want to mess up with that then just install this package. It requires minimal configuration.
有时您已经编写了测试用例并且您不想弄乱它然后安装这个包。它需要最少的配置。
composer require laravel/browser-kit-testing --dev
For more info refer Laravel Browserkit Testing
有关更多信息,请参阅Laravel Browserkit 测试