【发布时间】:2017-07-11 23:44:21
【问题描述】:
我遇到了在 Laravel 5.4 上设置的 behat / mink 的问题,它无法从模型中的函数返回值,我正在寻找一些关于如何解决这个问题的建议。
下面是一个非常基本的 laravel 安装示例:
例如。我有一个名为 User.php 的模型,在其中我有以下功能
public static function someFunction(){
return 'A Random Text String';
}
在我的 home.controller 中,我为 index() 定义了以下内容
例如
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = Auth::user();
return view('home',compact('user'));
}
在相应的刀片文件中,我有以下内容
{{$user}}
{{$user->someFunction()}}
我有一个功能设置如下:
Scenario: Home Page # features/hometest.feature:6
Given I am on the homepage # FeatureContext::iAmOnHomepage()
Then I should see "Laravel" # FeatureContext::assertPageContainsText()
Scenario: Check we can login
When I go to "/register"
And I fill in "Name" with "Dave"
And I fill in "E-Mail Address" with "Dave@dave.com"
And I fill in "Password" with "password"
And I fill in "Confirm Password" with "password"
And I press "Register"
And I should see "You are logged in!"
Scenario: Check we can access the homepage as Dave
Given I am logged in as Dave
When I go to "/home"
And I should see "A Random Text String"
对应于我的 FeatureContext.php 中的一个函数
/**
* @Given /^(?:|I )am logged in as Dave$/
*/
public function iAmLoggedInAsDave()
{
$this->visit('/login');
$this->fillField('E-Mail Address', 'dave@dave.com');
$this->fillField('Password', 'password');
$this->pressButton('Login');
$this->printCurrentUrl();
}
如果我通过浏览器查看以上内容,我可以看到
{"id":1,"name":"Dave","email":"Dave@dave.com","created_at":"2017-06-30 16:10:39","updated_at":"2017-06-30 16:10:39"}
A Random Text String
这相当于我的测试通过了。
但是当我运行 behat 时,在我的 /home/vagrant/Code/client/client.app 目录中 ../vendor/behat/behat/bin/behat
我失败了
Scenario: Check we can login # features/hometest.feature:19
Given I am logged in as Dave # FeatureContext::iAmLoggedInAsDave()
│ http://localhost/home
When I go to "/home" # FeatureContext::visit()
And I should see "A Random Text String" # FeatureContext::assertPageContainsText()
The text "A Random Text String" was not found anywhere in the text of the current page. (Behat\Mink\Exception\ResponseTextException)
我的日志中出现以下错误。
Next ErrorException: Call to undefined method Illuminate\Database\Query\Builder::someFunction() (View: /home/vagrant/Code/client/client.app/resources/views/home.blade.php) in / home/vagrant/Code/client/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2450
我目前的开发平台是使用 vagrant 使用 homestead 搭建的。
behat.yml 中有以下内容
default:
extensions:
Laracasts\Behat:
env_path: .env.behat
Behat\MinkExtension:
default_session: laravel
laravel: ~
【问题讨论】:
标签: php laravel-5 tdd behat mink