首页
/ Pickle 项目技术文档

Pickle 项目技术文档

2024-12-23 19:20:40作者:韦蓉瑛

1. 安装指南

环境要求

  • Ruby 版本:>= 0
  • Rails 版本:5.x, 6.x, 7.x
  • Cucumber 版本:3.x 到 9.x

安装步骤

  1. 首先,确保你已经安装了 cucumber-rails 及其依赖项。
  2. 在 Gemfile 中添加以下内容:
    gem 'pickle'
    
  3. 运行以下命令安装 gem:
    bundle install
    
  4. 运行生成器以查看可用选项:
    rails g pickle --help
    
  5. 运行生成器以生成相关文件,例如:
    rails g pickle --paths --email
    

2. 项目使用说明

快速开始

Pickle 提供了方便的 Cucumber 步骤,用于通过 factory-girlmachinistfabrication 轻松创建模型。你也可以使用 ActiveRecord 作为工厂,但功能不如前述工具强大。

使用步骤

  1. 查看生成的 features/step_definitions/pickle_steps.rb 文件。
  2. 如果需要路径步骤和电子邮件步骤,可以在生成器中添加 paths 和/或 email 选项。生成的代码将分别写入 features/env/paths.rbfeatures/step_definitions/email_steps.rb

使用不同的 ORM

Pickle 支持 Active Record、DataMapper 和 Mongoid。例如,如果你有一个名为 Post 的模型,带有 titlebody 字段,你可以编写如下步骤:

Given a post exists with title: "My Post", body: "My body"

使用 factory-girl 或 machinist

如果你使用 MachinistFactoryBot,可以通过以下方式利用你编写的工厂/蓝图:

Given a user exists
And another user exists with role: "admin"

配置 Pickle

你可以在 features/support/pickle.rb 中配置 Pickle,例如:

Pickle.configure do |config|
  config.adapters = [:machinist, :active_record, YourOwnAdapterClass]
  config.map 'me', 'myself', 'my', 'I', :to => 'user: "me"'
end

3. 项目 API 使用文档

提供的步骤

Given 步骤

  • Given a model exists:创建一个模型实例。
  • Given a model exists with fields:创建一个带有指定字段的模型实例。
  • Given n models exist:创建多个模型实例。
  • Given n models exist with fields:创建多个带有指定字段的模型实例。
  • Given the following models exist:通过表格创建多个模型实例。

Then 步骤

  • Then a model should exist:断言模型存在。
  • Then a model should exist with fields:断言模型存在并带有指定字段。
  • Then n models should exist:断言多个模型存在。
  • Then n models should exist with fields:断言多个模型存在并带有指定字段。
  • Then the following models exist:通过表格断言多个模型存在。

自定义步骤

你可以使用 Pickle 提供的正则表达式来创建自定义步骤,例如:

Given /^"(\w+)" is the father of "(\w+)"$/ do |father, child|
  Fatherhood.create! :father => model!("person: \"#{father}\""), :child => model!("person: \"#{child}\"")
end

4. 项目安装方式

通过 Gemfile 安装

在 Gemfile 中添加:

gem 'pickle'

然后运行:

bundle install

生成器使用

运行生成器以生成相关文件:

rails g pickle --paths --email

通过以上步骤,你可以轻松安装并使用 Pickle 项目,享受其提供的便捷 Cucumber 步骤和灵活的配置选项。

登录后查看全文
热门项目推荐