brunty/laravel-behat-fixtures

此包已被弃用且不再维护。未建议替代包。

提供了一种在Laravel中使用Behat时将固定数据加载到数据库中的方法

0.2 2017-03-25 20:55 UTC

This package is auto-updated.

Last update: 2022-06-23 11:10:47 UTC


README

安装

此包要求您已安装并配置好 Laracasts: Behat Laravel Extension。由于它依赖于该扩展来为我们加载和设置Laravel。

composer require brunty/laravel-behat-fixtures --dev

配置

将固定数据包含在您的测试套件中,并传递固定数据文件夹的路径

default:
    suites:
        default:
            contexts:
                - Brunty\LaravelBehatFixtures\FixtureContext:
                    - '%paths.base%/features/bootstrap/fixtures/'

用法

重置和刷新数据库

此包提供对使用 BeforeScenario 钩子的标签的访问。在每个场景之前刷新数据库迁移,使用 @db-refresh 在每个功能场景之前或在单独的场景中使用

# Will be called before each scenario in the feature
@db-refresh
Feature: My amazing feature
  # Will be called before only scenarios tagged with this
  @db-refresh
  Scenario: Things
    Given nothing is happening
    When nothing happens
    Then nothing should have happened

固定数据

在固定数据文件夹中创建文件,以便将它们加载到功能和场景中。这些只是普通的PHP文件,因此您可以使用Laravel的 factory() 方法或使用模型上的Eloquent方法来创建事物

%paths.base%/features/bootstrap/fixtures/users.base.php

<?php

// Create users via factories
$users = factory(App\User::class, 5)->create();

// Alternatively, you can just do whatever you'd like with eloquent
$user = new \App\User(['name' => 'Matt', 'email' => 'thing@thing.com', 'password' => bcrypt('apassword')]);
$user->save();

然后,您可以使用 @fixture 标签加载这些固定数据文件,其中标签内容是固定数据文件名(不包括 .php 扩展名)

@db-refresh
Feature:

  @fixture(users.base)
  Scenario: Things
    Given nothing is happening
    When nothing happens
    Then nothing should have happened

如果您想为场景加载多个固定数据,可以这样做

@db-refresh
Feature:

  @fixture(users.base) @fixture(users.active)
  Scenario: Things
    Given nothing is happening
    When nothing happens
    Then nothing should have happened

这些将按以下顺序加载:首先是 users.base.php,然后是 users.active.php

贡献

这个项目最初是周六晚上无聊时开始的一个项目,如果您在使用这个项目,并且想要更多功能,请随时提出建议或提交PR!

尽管这个项目很小,但开放性和包容性被认真对待。为此,已采用以下行为准则。

贡献者行为准则