[英]Use Laravel Eloquent from within Behat/Mink FeatureContext
這個問題假設您對 Laravel、Behat 和 Mink 有一定的了解。
考慮到這一點,我無法從我的 Behat FeatureContext 文件中對數據庫進行簡單調用,該文件看起來有點像這樣......
<?php
use App\Models\Db\User;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context {
public function __construct() {}
/**
* @Given I am authenticated with :email and :password
*/
public function iAmAuthenticatedWith($email, $password) {
User::where('email', $email)->firstOrFail();
$this->visitPath('/login');
$this->fillField('email', $email);
$this->fillField('password', $password);
$this->pressButton('Login');
}
}
當此場景運行時,我收到此錯誤...
Fatal error: Call to a member function connection() on null (Behat\Testwork\Call\Exception\FatalThrowableError)
這是由這條線引起的......
User::where('email', $email)->firstOrFail();
如何在 Behat/Mink FeatureContext 中使用 Laravel Eloquent(進行數據庫調用)? 我需要在我的 FeatureContext 的構造函數中公開一些東西嗎? 在composer.json
或behat.yml
文件中更新/添加一行?
如果有不止一種方法可以解決這個問題並且值得一提,請這樣做。
額外細節
Laravel: 5.5.*
行為: ^3.3
貂皮擴展: ^2.2
Mink Selenium 2 驅動程序: ^1.3
行為配置
default:
extensions:
Behat\MinkExtension\ServiceContainer\MinkExtension:
base_url: "" #omitted
default_session: selenium2
selenium2:
browser: chrome
Laravel 需要設置 eloquent 和連接才能使其工作。
簡單的方法是擴展 laravel TestCase
並在__constructor()
調用parent::setUp();
它會設置你的測試環境,就像你在 Laravel 中運行 php 測試單元一樣:
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
{
if (! $this->app) {
$this->refreshApplication();
}
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback) {
call_user_func($callback);
}
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
}
refreshApplication()
將調用createApplication()
並引導 Laravel 並創建$this->app
對象。
/**
* Refresh the application instance.
*
* @return void
*/
protected function refreshApplication()
{
$this->app = $this->createApplication();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.