【发布时间】:2015-04-21 14:31:44
【问题描述】:
我正在关注 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
我尝试在错误的行上使用“页面”而不是“响应”,我也尝试使用其他链接而不是“成员/显示”,但什么也没做。
现在有人有什么想法吗?
编辑
我应该使用它绝对是“页面”而不是“响应”,但这仍然不能清除错误消息。
【问题讨论】:
-
难道我不能在请求规范中使用 render_template 吗?
-
我认为
render_template更适合控制器规格 -
这会是 Rails 4 的新功能吗?因为我遵循的教程说将该测试放在请求规范中,并且该教程是关于 Rails 3 的。
-
对不起,我把它和特性规范混在一起了,无论如何,我仍然认为如果不运行
get或post请求,你不会得到响应对象,第二个期望是有道理的,但在用page替换response就像你说的那样,第一个期望应该移到用户控制器规范中才能工作 -
我发现了一些东西。它说here render_template 应该在我的版本 rspec 3.2 中的请求规范中工作。它还说render_template 使用rails 的函数assert_template。我去那里问问。