如何用 Behat 和 Mockery 交换 Laravel 实现

How to swap Laravel implementation with Behat and Mockery

我有一个 Behat FeatureContext,我想将给定 class 的 Laravel 实现替换为模拟的

所以我有这个方法,带有 @beforeSuite 注释

/**
     * @static
     * @beforeSuite
     */
    public static function mockData()
    {
        $unitTesting = true;
        $testEnvironment = 'acceptance';

        $app = require_once __DIR__.'/../../../bootstrap/start.php';
        $app->boot();

        $fakeDataRetriever = m::mock('My\Data\Api\Retriever');


        $fakeData = [
           'fake_name' => 'fake_value'
        ];

        $fakeDataRetriever->shouldReceive('getData')->andReturn($fakeData);

        $app->instance('My\Data\Api\Retriever', $fakeDataRetriever);

    }

所以我看到 Laravel 应用程序和假数据被交换了,但是当我 运行 Behat 时,它被忽略了,这意味着 Laravel 正在使用实际的实现而不是假的。

我正在使用 Laravel 4.2

有人知道在 运行ning Behat 时交换 Laravel 实现的方法吗?

我需要这个的原因是因为数据来自远程 API,我希望测试 运行 而不会触及 API。

我对 Behat 不太熟悉,除了我刚刚在快速教程中阅读的内容以查看是否可以在此处找到帮助... http://code.tutsplus.com/tutorials/laravel-bdd-and-you-lets-get-started--cms-22155

看起来您正在创建 Laravel 的新实例,在其中设置实例实现,然后您没有对 Laravel 实例执行任何操作。接下来可能发生的是测试环境继续进行并使用它自己的 Laravel 实例来 运行 测试。

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use PHPUnit_Framework_Assert as PHPUnit;
use Symfony\Component\DomCrawler\Crawler;

use Illuminate\Foundation\Testing\ApplicationTrait;

/**
 * Behat context class.
 */
class LaravelFeatureContext implements SnippetAcceptingContext
{
    /**
     * Responsible for providing a Laravel app instance.
     */
    use ApplicationTrait;

    /**
     * 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()
    {
    }

    /**
     * @BeforeScenario
     */
    public function setUp()
    {
        if ( ! $this->app)
        {
            $this->refreshApplication();
        }
    }

    /**
     * Creates the application.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;

        $testEnvironment = 'testing';

        return require __DIR__.'/../../bootstrap/start.php';
    }

    /**
     * @static
     * @beforeSuite
     */
    public function mockData()
    {
        $fakeDataRetriever = m::mock('My\Data\Api\Retriever');

        $fakeData = [
            'fake_name' => 'fake_value'
        ];

        $fakeDataRetriever->shouldReceive('getData')->andReturn($fakeData);

        $this->app->instance('My\Data\Api\Retriever', $fakeDataRetriever);
    }
}