Rails/Cucumber/Webrat:redirect_to、flash[:notice] 不起作用

发布于 2024-08-19 04:06:16 字数 1473 浏览 2 评论 0原文

我是 Cucumber 的新手,一直在逐步学习 Ryan Bates 的 Railscast。 http://railscasts.com/episodes/155-beginning-with-cucumber

不幸的是,我的场景在railscast 经过的地方失败了。具体来说,它在以下步骤中失败:然后我应该看到“已创建新文章。”

我怀疑这可能与我们正在使用的宝石的不同版本有关,目前我有最新的每个。

它给了我以下错误:

*然后我应该看到“新文章已创建”。 期望以下元素的内容包含“已创建新文章。”:(

Title
Content

Spec::Expectations::ExpectationNotMetError) ./features/step_definitions/web_steps.rb:144:in /^(?:|I ) 应该看到 "([^\"]*)"$/' features/manage_articles.feature:18:in然后我应该看到“New Article Created”。'*

这是来源:

manage_articles.feature

功能:管理文章

      场景:创建有效文章
        鉴于我没有文章
        我在文章列表中
        当我关注“新文章”时
        我用“马铃薯”填写“标题”
        我在“内容”中填写“美味的土豆”
        然后我应该看到“新文章已创建”。
        我应该看看“土豆”
        我应该看看“美味土豆”
        我应该有 1 篇文章

文章_controller.rb

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

索引.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 %>

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:in
Then 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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-08-26 04:06:16

调试 Cucumber 的一个好技巧是创建一些调试步骤。

在 debug_steps.rb 文件中,我有以下内容:

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

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

注意,save_and_open_page 需要:
Webrat:webrat (0.5.3)
和 Launchy: launchy (0.3.3)

然后添加步骤:

然后我打开页面

然后然后我应该看到“新文章已创建。”

看看发生了什么。

祝你好运。希望这有帮助。

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.

最美的太阳 2024-08-26 04:06:16

我认为您必须在然后我应该看到“New Article Created”之前添加此行。

And I press "Create"

所以,这是您的完整场景:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文