1

我正在关注 Hartl 的 Ruby on Rails 教程,但无法弄清楚为什么以下代码不断给我这个错误:

Failure/Error: expect(response).to render_template("membres/show")
     NoMethodError:
       undefined method `body' for nil:NilClass

编码:

require 'rails_helper'

RSpec.describe "Membres", :type => :request do  

  include Capybara::DSL

  describe "une inscription" do
    describe "ratée" do
      it "ne devrait pas créer un nouvel utilisateur" do
        visit signup_path
        fill_in "Nom",          :with => ""
        fill_in "Email",        :with => ""
        fill_in "Password",     :with => ""
        fill_in "Confirmation", :with => ""
        click_button "Inscription"
        expect(response).to render_template("membres/show")
        expect(response).to have_tag("div#error_explanation")
      end
    end
  end
end

我尝试在错误的行上使用“页面”而不是“响应”,我还尝试了其他链接而不是“成员/显示”,但没有任何效果。

有人对此有任何想法吗?

编辑

我应该使用它绝对是“页面”而不是“响应”,但这仍然不能清除错误消息。

4

1 回答 1

0

这是解决该问题的一种方法。

在您的错误消息中查找“test_case.rb”的路径和错误行:

NoMethodError:
       undefined method `body' for nil:NilClass
     # /var/lib/gems/1.9.1/gems/actionpack-4.2.0/lib/action_controller/test_case.rb:111:in `assert_template'
     # ./spec/requests/membres_spec.rb:21:in `block (5 levels) in <top (required)>'
     # ./spec/requests/membres_spec.rb:23:in `block (4 levels) in <top (required)>'

在我的情况下,该文件位于“/var/lib/gems/1.9.1/gems/actionpack-4.2.0/lib/action_controller/”,错误行是 111。

使用您最喜欢的文本编辑器,以 root 身份打开“test_case.rb”,转到指定的行,它应该如下所示:

response.body

将其更改为:

response.body if !response.nil?

而且您应该能够使用 render_template 或 assert_template 而不使用:

get

或者

post

就我而言,我可以使用

visit

这不会创建“响应”对象,因此会出现错误消息。

于 2015-03-03T03:52:10.467 回答