Ruby on Rails:paths.rb 的 Cucumber 自定义定义

发布于 2024-11-16 08:32:21 字数 536 浏览 3 评论 0原文

我试图学习 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" 

I trying to learn BDD way for development and just watched RailsCasts lesson for Cucumber. There i've seen approach to describe some actions like:

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

And, as i understand all those "I go to" and "I should see" constructions hardcoded somewhere. So in paths.rb i can write:

def path_to(page_name)
case page_name

when /the list of articles/
  articles_path

And it'll recognize this path next time automaticaly. And "i should see" has same feature.

So, the question: is there ant way to replace those "I go to" and "I should see" constructions with another language or custom sequences? For example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-11-23 08:32:21

当然,

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 

希望这会有所帮助。

Sure,

Cucumber uses the steps in web_steps.rb to make the call to the mapping. It looks like:

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

So, I would write the following step that redirect to the standard 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 

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文