gpt4 book ai didi

ruby-on-rails - 无法让 RSpec View 规范通过

转载 作者:行者123 更新时间:2023-11-28 20:05:11 24 4
gpt4 key购买 nike

我在 spec/views/users/new.html.erb_spec.rb 中有以下规范:

require 'spec_helper'

describe "users/new.html.erb" do
it "displays the text attribute of the message" do
render
response.should contain("Register")
end
end

但是当我运行测试时它失败了:

ActionView::TemplateError in 'users/new.html.erb displays the text attribute of the message'
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

失败的行是:

<% form_for @user, :url => account_path do |f| %>

new 方法的 Users Controller 中,我有这个:

@user = User.new

知道我为什么会收到该错误吗?

更新:根据要求,这是我的路线文件...

ActionController::Routing::Routes.draw do |map|
map.resource :account, :controller => "users"
map.resources :users

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

最佳答案

View 规范与用户 Controller 完全隔离运行。因此,您必须自己初始化 View 中所需的变量,如所述 here .结果将是这样的:

require 'spec_helper'
require 'path/to/user.rb'

describe "users/new.html.erb" do
it "displays the text attribute of the message" do
assigns[:user] = User.new
render
response.should contain("Register")
end
end

如果您想将您的 View 与您的 Controller 一起测试,我建议您考虑使用 Cucumber 进行集成测试。

关于ruby-on-rails - 无法让 RSpec View 规范通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3806049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com