测试你在Cucumber和Capybara的页面上

时间:2013-03-08 12:49:36

标签: ruby-on-rails cucumber capybara

在Aslak Hellesoy的The training wheels came off帖子中,他说他已经从更新版本的黄瓜中删除了web_steps.rb和paths.rb。

我可以理解使用Capybara api而不是web_steps.rb,但您现在如何测试您是否在特定页面上?

这就是我以前用paths.rb做的方式:

#admin_authentication.feature    
Then  I should be on the admin home page

# paths.rb
when /the admin home page/
  admin_root_path

# web_steps.rb
Then /^(?:|I )should be on (.+)$/ do |page_name|
  current_path = URI.parse(current_url).path
  if current_path.respond_to? :should
    current_path.should == path_to(page_name)
  else
    assert_equal path_to(page_name), current_path
  end
end

作为次要问题,我们应该这样做吗?

2 个答案:

答案 0 :(得分:13)

我只是这样做,看看它是否适合你:

assert page.current_path == admin_root_path

答案 1 :(得分:8)

通常,您不应该在Cucumber步骤中测试页面路径。黄瓜场景应该从用户的角度编写。用户通常只对页面内容感兴趣,因此测试应该检查页面内容而不是特定的URL或控制器。

而不是像开发人员的观点那样编写一个场景:

Scenario: Administrator should be on the admin home page
  When I log in as an administrator
  Then I should be on the admin home page

从用户的角度写出来:

Scenario: Administrator should have super-user tools
  When I log in as an administrator
  Then I should see the disable-abusive-user button