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

2019-02-20 19:27发布

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 %>

2条回答
SAY GOODBYE
2楼-- · 2019-02-20 20:03

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
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-20 20:06

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.

查看更多
登录 后发表回答