1

我已经参考了链接http://railscasts.com/episodes/155-beginning-with-cucumber进行测试。

当我before_filter :authenticate_user!在我的控制器中使用时,我的所有案例都失败了,当我评论before_filter我的所有测试案例都通过了。我收到以下错误。

Scenario: Create Valid Article   
          Given I have no articles                                
          And I am on the list of articles                       
          When I follow "New Article"                             

 Unable to find link "New Article" (Capybara::ElementNotFound)
  ./features/step_definitions/article_steps.rb:25:in `/^I follow "([^\"]*)"$/'
  features/manage_articles.feature:15:in `When I follow "New Article"'

  And I fill in "article[title]" with "Spuds"        
  And I fill in "Content" with "Delicious potato wedges!" 
  And I press "Create"                                   
  Then I should see "New article created."                
  And I should see "Spuds"                                
  And I should see "Delicious potato wedges!"             
  And I should have 1 article                             

 Failing Scenarios:
   cucumber features/manage_articles.feature:12 # Scenario: Create Valid Article

2 scenarios (1 failed, 1 passed)
14 steps (1 failed, 7 skipped, 6 passed)

article_steps.rb

Given /^I have articles titled (.+)$/ do |titles|
  titles.split(', ').each do |title|
   Article.create!(:title => title)
  end
end

When /^I go to the list of articles$/ do
 visit articles_path
end

Then /^I should see "(.*?)"$/ do |arg1|

end

Given /^I have no articles$/ do
 Article.delete_all
end

Given /^I am on the list of articles$/ do
  visit articles_path
end

When /^I follow "([^\"]*)"$/ do |link|
  click_link(link)
end

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field, :with => value)
end

When /^I press "([^\"]*)"$/ do |button|
  click_button(button)
end

Then /^I should have ([0-9]+) articles?$/ do |count|
  Article.count.should == count.to_i
end
4

2 回答 2

1

由于您的身份验证失败,因此您的测试场景应该类似于首先对用户进行身份验证......因为没有身份验证它不会进入文章列表页面,因此“新文章”链接尚未找到并出现该错误

Scenario: Create Valid Article 
      When I am on the login page
      Then I filled up username as "abc"
      Then I filled up password as "abc"
      Then I follow "login" 
      Given I have no articles                                
      And I am on the list of articles                       
      When I follow "New Article" 

或者

 Scenario: Create Valid Article 
      Given User is authenticated        
      Given I have no articles                                
      And I am on the list of articles                       
      When I follow "New Article"
于 2013-09-19T12:40:35.577 回答
1

由于错误,capybara 在页面上找不到链接。所以根据@gotva,请检查页面上是否有“新文章”的链接。

正如您所说before_filter :authenticate_user!,这意味着您需要在尝试访问该页面之前登录。但是该功能中没有登录场景。

所以我建议你按照@Rajarshi Das 的建议在页面中添加登录场景。如果您的登录场景总是包含在每个功能中,那么您可以将其放在Background中。

Feature: Managing Articles

Background:
  Given user is logged in

Scenario: Create Valid Article   
  Given I have no articles                                
  And I am on the list of articles                       
  When I follow "New Article"                             
  And I fill in "article[title]" with "Spuds"        
  And I fill in "Content" with "Delicious potato wedges!" 
  And I press "Create"                                   
  Then I should see "New article created."                
  And I should see "Spuds"                                
  And I should see "Delicious potato wedges!"             
  And I should have 1 article            


common_steps.rb

def login user
  #stuff of login process
end

Given /^user is logged in$/ do
  @user = Factory.create(:user) #if you are using factory_girl
  login(@user)
end

希望有帮助!!!

于 2013-09-19T13:07:43.380 回答