【发布时间】:2014-10-12 20:09:19
【问题描述】:
我正在努力将 Behat 3 包含在一个基于 Laravel 的新 API 中,并且我正在使用 lucadegasperi/oauth2-server-laravel 包来处理身份验证。
一直在努力让 Behat 设置正常工作(现在是这样)。我不明白为什么,但似乎我需要在 beforeSuite 和 beforeFeature 挂钩中迁移包。似乎很愚蠢,因为我只需要在所有功能运行之前迁移一次..?
我只想在套件加载之前迁移一次,否则随着测试数量的增加,运行时间可能会变长。
我一直在编写 Behat 2.* 的示例以在版本 3 中工作。
我的 FeatureContext 类目前看起来像这样:
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
/**
* Behat context class.
*/
class FeatureContext implements SnippetAcceptingContext
{
private $_restObject = null;
private $_restObjectType = null;
private $_restObjectMethod = 'get';
private $_client = null;
private $_response = null;
private $_requestUrl = null;
private $_baseUrl = null;
/**
* Initializes context.
*
* Every scenario gets its own context object.
* You can also pass arbitrary arguments to the context constructor through behat.yml.
*/
public function __construct($baseUrl)
{
$this->_restObject = new stdClass();
$this->_client = new GuzzleHttp\Client();
$this->_baseUrl = $baseUrl;
}
/**
* @static
* @beforeSuite
*/
public static function bootstrapLaravel()
{
$unitTesting = true;
$testEnvironment = 'testing';
// This assumes the FeatureContext.php class is within app/tests/features/bootstrap
$app = require_once __DIR__.'/../../../../bootstrap/start.php';
$app->boot();
Mail::pretend(true);
}
/**
* @static
* @beforeSuite
*/
public static function setUpDb()
{
Artisan::call('migrate');
self::migratePackages();
/* do seeding */
$seeders = array(
"OAuthTestSeeder",
"RolesPermissionsSeeder"
);
foreach ($seeders as $seedClass)
{
Artisan::call("db:seed", array("--class" => $seedClass));
}
}
/**
* @static
* @beforeFeature
*/
public static function prepDb()
{
Artisan::call('migrate:refresh');
self::migratePackages();
Artisan::call('db:seed');
}
public static function migratePackages()
{
$packages = array(
"lucadegasperi/oauth2-server-laravel",
);
foreach ($packages as $packageName)
{
Artisan::call("migrate",
array("--package" => $packageName, "--env" => "testing"));
echo 'migrating package: '.$packageName;
}
}
/* feature specs here...
*/
}
【问题讨论】:
-
如果我知道您在问什么,我会提供帮助 :) 您是否尝试在所有测试开始前只迁移一次数据库?
-
谢谢@IanBytchek 我用更清晰的问题更新了问题!是的,回答你的问题。还想知道我是否以理智/有效的方式来做这件事,因为很难找到与 Behat 3 兼容的示例。
-
你最后解决了吗?