我已提到链接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)
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
答案 0 :(得分: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"
答案 1 :(得分:1)
基于错误,水豚无法在页面上找到链接。所以根据@gotva,请检查页面上是否有“新Aritcle”的链接。
正如您放置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
希望有所帮助!!!