Ruby on Rails:paths.rb的黄瓜自定义定义

时间:2011-06-20 20:08:13

标签: ruby-on-rails ruby cucumber bdd

我正在尝试学习BDD开发方式,并且只是观看了Cucumber的RailsCasts课程。我已经看到了描述一些行为的方法:

When I go to the list of articles
Then I should see "Pizza"

而且,据我了解所有那些“我去”和“我应该看到”的结构在某处硬编码。所以在paths.rb中我可以写:

def path_to(page_name)
case page_name

when /the list of articles/
  articles_path

下次自动识别这条路径。并且“我应该看到”具有相同的功能。

所以,问题是:是否有蚂蚁方法用另一种语言或自定义序列替换那些“我去”和“我应该看到”的构造?例如:

When I constantly visiting the list of articles
Then I have to observe text "Pizza" 

1 个答案:

答案 0 :(得分:2)

当然,

Cucumber使用web_steps.rb中的步骤来调用映射。它看起来像:

When /^(?:|I )go to (.+)$/ do |page_name|
    visit path_to(page_name)
end

Then /^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
  with_scope(selector) do
    if page.respond_to? :should
      page.should have_content(text)
    else
      assert page.has_content?(text)
    end
  end
end

所以,我会编写以下步骤,重定向到标准web_steps ...

#step_definitions/my_steps.rb

When /^(?:|I )constantly visiting (.+)$/ do |page_name|
   When %{I go to #{page_name}}
end

Then /^(?:|I )have to observe text "([^"]*)" do |text|
   Then %{I should see "#{text}"}
end 

希望这有帮助。