了解 RSpec 失败

发布于 2024-10-05 06:25:12 字数 2376 浏览 1 评论 0原文

我在运行 rspec spec/ 时遇到这些失败。失败的规范是使用脚手架自动生成的。我正在尝试理解 RSpec,但我不知道从哪里开始寻找原因,除了感觉缺少某些方法?!?然而,应用程序似乎运行良好。 test.log 中没有显示有关这些失败的信息。我是否应该在其他地方寻找提示来追踪这个问题?

$ rspec spec/
.....................F.F.

Failures:

  1) clowns/edit.html.haml renders the edit clown form
     Failure/Error: render
     undefined method `to_sym' for nil:NilClass
     # ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/edit.html.haml:3:in `_app_views_clowns_edit_html_haml___574620942879655923_2176081980_599706797287605391'
     # ./spec/views/clowns/edit.html.haml_spec.rb:13:in `block (2 levels) in <top (required)>'

  2) clowns/new.html.haml renders new clown form
     Failure/Error: render
     undefined method `to_sym' for nil:NilClass
     # ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/new.html.haml:3:in `_app_views_clowns_new_html_haml__1085372210838170129_2159651900_599706797287605391'
     # ./spec/views/clowns/new.html.haml_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 1.03 seconds
27 examples, 2 failures, 2 pending

这是显然失败的规范(edit.html.haml_spec.rb)。它是由 rails gscaffold Clown name:string bubbles:integer 自动生成的:

#spec/views/clowns/edit.html.haml_spec.rb
require 'spec_helper'

describe "clowns/edit.html.haml" do
  before(:each) do
    @clown = assign(:clown, stub_model(Clown,
      :name => "MyString",
      :balloons => 1
    ))
  end

  it "renders the edit clown form" do
    render

    # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
    assert_select "form", :action => clown_path(@clown), :method => "post" do
      assert_select "input#clown_name", :name => "clown[name]"
      assert_select "input#clown_balloons", :name => "clown[balloons]"
    end
  end
end

I'm getting these failures when running rspec spec/. The spec that is failing is was auto-generated with the scaffolding. I'm trying to understand RSpec but I don't know where to begin looking for the cause other than it feels like some method is missing?!? Yet, app appears to be working fine. Nothing about these failures appears in test.log. Is there another place I should be looking for hints to track this down?

$ rspec spec/
.....................F.F.

Failures:

  1) clowns/edit.html.haml renders the edit clown form
     Failure/Error: render
     undefined method `to_sym' for nil:NilClass
     # ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/edit.html.haml:3:in `_app_views_clowns_edit_html_haml___574620942879655923_2176081980_599706797287605391'
     # ./spec/views/clowns/edit.html.haml_spec.rb:13:in `block (2 levels) in <top (required)>'

  2) clowns/new.html.haml renders new clown form
     Failure/Error: render
     undefined method `to_sym' for nil:NilClass
     # ./app/views/clowns/_form.html.haml:4:in `block in _app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/_form.html.haml:1:in `_app_views_clowns__form_html_haml__3590088286240866241_2176114460_3896491916910336970'
     # ./app/views/clowns/new.html.haml:3:in `_app_views_clowns_new_html_haml__1085372210838170129_2159651900_599706797287605391'
     # ./spec/views/clowns/new.html.haml_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 1.03 seconds
27 examples, 2 failures, 2 pending

And here is the spec that apparently fails (edit.html.haml_spec.rb). It was auto-generated by rails g scaffold Clown name:string balloons:integer:

#spec/views/clowns/edit.html.haml_spec.rb
require 'spec_helper'

describe "clowns/edit.html.haml" do
  before(:each) do
    @clown = assign(:clown, stub_model(Clown,
      :name => "MyString",
      :balloons => 1
    ))
  end

  it "renders the edit clown form" do
    render

    # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
    assert_select "form", :action => clown_path(@clown), :method => "post" do
      assert_select "input#clown_name", :name => "clown[name]"
      assert_select "input#clown_balloons", :name => "clown[balloons]"
    end
  end
end

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

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

发布评论

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

评论(1

甜`诱少女 2024-10-12 06:25:12

您是否也在使用simple_form(或者甚至formtastic)?当我将脚手架表单从标准表单转换为 simple_form 表单时,我遇到了同样的错误 (form_for(@user) => simple_form_for(@user))。

正如你所说,它仍然有效,所以我不确定是否值得担心。我认为你最好让 Cucumber 去担心这件事。 Sarah Mei 表示查看规范是浪费时间 。 :-)

Are you also using simple_form (or perhaps even formtastic)? I get the same error when I convert the scaffold forms from standards forms to simple_form forms (form_for(@user) => simple_form_for(@user)).

It still works, as you say, so I'm not sure it's worth worrying about. I think you'd be better off just letting Cucumber worry about it. Sarah Mei says view specs are a waste of time. :-)

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