Rails/Cucumber/Webrat: redirect_to, flash[:notice] not working

為{幸葍}努か 提交于 2019-12-19 11:53:17

问题


I'm new to Cucumber and have been stepping through a railscast by Ryan Bates. http://railscasts.com/episodes/155-beginning-with-cucumber

Unfortunately my scenario is failing where the railscast passes. Specifically it is failing on the step: Then I should see "New Article Created."

I suspect it may have something to do with the differing versions of the gems we are using, currently I have the latest of each.

It gives me the following error:

*Then I should see "New Article Created." expected the following element's content to include "New Article Created.":

Title
Content

(Spec::Expectations::ExpectationNotMetError) ./features/step_definitions/web_steps.rb:144:in /^(?:|I )should see "([^\"]*)"$/' features/manage_articles.feature:18:inThen I should see "New Article Created."'*

This is the source:

manage_articles.feature

Feature: Manage Articles

      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 "Title" with "Spuds"
        And I fill in "Content" with "Delicious potatoes"
        Then I should see "New Article Created."
        And I should see "Spuds"
        And I should see "Delicious potatoes"
        And I should have 1 article

articles_controller.rb

  ...
  def create
    @article = Article.create!(params[:article])
    flash[:notice] = "New Article Created."
    redirect_to articles_path
  end

index.html.erb

<p><%= flash[:notice] %></p>
<% for article in @articles %>
    <p><%=h article.title %></p>
    <p><%=h article.content %></p>
<% end %>

<%= link_to "New Article", new_article_path %>

回答1:


I think you must add this line before Then I should see "New Article Created.":

And I press "Create"

So, here is your complete scenario:

Feature: Manage Articles

      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 "Title" with "Spuds"
        And I fill in "Content" with "Delicious potatoes"
        And I press "Create"
        Then I should see "New Article Created."
        And I should see "Spuds"
        And I should see "Delicious potatoes"
        And I should have 1 article



回答2:


A good trick to debugging cucumber is to create some debugging steps.

In a debug_steps.rb file I have the following:

Then /^I debug$/ do
 breakpoint; 0
end

Then /^I open the page$/ do
  save_and_open_page
end

Note, that save_and_open_page requires: Webrat: webrat (0.5.3) and Launchy: launchy (0.3.3)

Then add the step:

Then I open the page

before Then I should see "New Article Created."

To see what is going on.

Good luck. Hope this helps.



来源:https://stackoverflow.com/questions/2136334/rails-cucumber-webrat-redirect-to-flashnotice-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!