2013-03-08 67 views
4

The training wheels came off由Aslak Hellesoy发表,他说他已经从更新版本的黄瓜中删除了web_steps.rb和paths.rb。测试你在黄瓜和水豚的页面

我可以理解使用水豚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 

作为第二个问题,我们应该在所有这样做呢?

回答

8

通常情况下,你不应该测试在黄瓜步页路径。黄瓜情景应该是从用户的角度来写的。用户通常只对页面内容感兴趣,所以测试应该检查页面内容而不是特定的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 
+1

我认为这个答案更符合黄瓜精神和培训轮的文章 – 2013-03-09 20:53:41

13

我简单地做这样的,看它是否适合你:

assert page.current_path == admin_root_path 
+0

这个答案没有工作对我来说,虽然我不会使用它,因为它不是用户关注的 – 2013-03-09 20:55:48

+3

@Paul,你问的是水豚,这是我的答案中的水豚代码。水豚不是“用户关注的”,黄瓜是。我在这里让你感到困惑 – Benj 2013-03-09 23:52:08

+1

对不起,我的错...... – 2013-03-10 13:12:56