黄瓜试验中的种子数据

Cos*_*sti 17 ruby-on-rails cucumber

我正在写一个使用SaS的网络应用程序.每个客户都有自己的db和app目录,所以我有一个rake任务,它创建了运行他们网站的所有必要的最小数据(种子数据):默认权限和角色,superadmin用户,已经填充的"us_states"表,一些本地仓库和终端(这是一个物流应用程序).

我没有任何黄瓜方案,我刚刚开始构建一些.我是黄瓜初学者.

我首先将种子数据任务放在Given行中,但这几乎适用于所有场景,并且对于那些查看场景的非程序员来说没有多大意义(对于人类而言,它是如此给定的它不需要有意识地表达)所以我把它移到了hooks.rb.

我的第一个场景看起来像这样:

  1 Feature: Place an order
  2   In order to keep orders in the database
  3   As a admin
  4   I want to place orders
  5
  6 Scenario: Using common legs
  7   Given I have 1 customers
  8   And I'm on the homepage
  9   And I follow "Place an Order"
 10   When I select the customer
 11   And I select the SSLine
 12   And I click "Use Common Legs"
 13   Then I should see "PICKUP AT"
 14   And I should see "DELIVER TO" or "LOAD AT"
 15   And I should see EMPTY RETURN
Run Code Online (Sandbox Code Playgroud)

我的hooks.rb看起来像这样:

1 Before do
2   MinimumData.new('costi', '1234').populate  #username and password
3 end
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 我不想在每个场景之前运行此MinimumData.populate任务,因为它需要8秒.我应该只在全球范围内运行一次吗?怎么样?
  2. 我是否必须使用After.do清理数据库?我真的不想这样做,因为我将使用Model.delete_all语句复制After.do中的逻辑.我注意到在第一次运行之后,测试数据库仍然存在所有数据.我可以使用rake db:test:purge清除它并重新初始化它.这是一个好习惯吗?

Geo*_*tte 22

我不知道before(:all)黄瓜的等价物.您可以做的是将您的种子添加到文件中features/support/seeds.rb,然后在您features/support/env.rb的行顶部和下方需要environment.rb放置行:

require File.dirname(__FILE__) + '/seeds'
Run Code Online (Sandbox Code Playgroud)

或者

#just write the code you want to execute directly into the env.rb file
Run Code Online (Sandbox Code Playgroud)

这些是您可以添加的可用块 env.rb

Before do
  #this code is run before each scenario
end

after do
  #this code is run after each scenario
end

at_exit do
  #this code is run at the end
end
Run Code Online (Sandbox Code Playgroud)


dmo*_*oly 7

对Geoff Lanotte的答案进行了一次修正.它应该是

Before do
  # this code is run before each scenario
end

与首都B.

但是,您可能希望将它放在feature/support目录中的新文件中,而不是将此类代码放在env.rb文件中,例如"hooks.rb"文件.这是因为如果升级cucumber-rails,则会自动重新生成env.rb文件.

更多信息请访问:https://github.com/cucumber/cucumber/wiki/Hooks