Rspec redirect_to 给出 'comparison of Array with Array field' 错误
Rspec redirect_to giving 'comparison of Array with Array field' error
在我的 rails 应用程序中,我正在尝试进行一项基本测试,测试在创建文件夹后,用户是否被重定向到文件夹显示页面。我已经实现了这段代码,当我通过浏览器完成这一切时它可以工作但是测试失败并给我这个错误:
Failure/Error: response.should redirect_to folder_path(folder)
ArgumentError:
comparison of Array with Array failed
我撬开了这个测试块,它也是这样说的:
ArgumentError: comparison of Array with Array failed
from /Users/XXXX/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0/lib/action_dispatch/journey/formatter.rb:43:in `sort'
有人知道为什么会出现这个错误吗?
下面是供参考的试块:
context "with valid inputs" do
let(:alice) { Fabricate(:user) }
let(:folder) { Fabricate.attributes_for(:folder) }
before do
login_user(alice)
post :create, folder: folder
end
it "redirects to the folder show page" do
response.should redirect_to folder_path(folder)
end
以及对应的控制器代码:
def create
new_folder(folder_params)
if @folder.save
flash[:success] = "Folder Created"
redirect_to folder_path(@folder)
else
flash[:danger] = "An Error occured."
render :new
end
end
如评论中所述,Fabricate.attributes_for
不是创建模型实例,而是创建模型所有属性的散列(不包括 id 属性)。因此,当您将文件夹传递给 folder_path
时,rails 正在寻找哈希的 ID。
这是一种测试重定向的方法:
it "redirects to the folder show page" do
response.should redirect_to folder_path(Folder.last)
end
还要确保最后一个文件夹是您想要的文件夹:
it "creates a folder" do
Folder.last.attributes.except(:id).each do |key, value|
folder[key].should eq(value)
end
end
在我的 rails 应用程序中,我正在尝试进行一项基本测试,测试在创建文件夹后,用户是否被重定向到文件夹显示页面。我已经实现了这段代码,当我通过浏览器完成这一切时它可以工作但是测试失败并给我这个错误:
Failure/Error: response.should redirect_to folder_path(folder)
ArgumentError:
comparison of Array with Array failed
我撬开了这个测试块,它也是这样说的:
ArgumentError: comparison of Array with Array failed
from /Users/XXXX/.rvm/gems/ruby-2.1.5/gems/actionpack-4.2.0/lib/action_dispatch/journey/formatter.rb:43:in `sort'
有人知道为什么会出现这个错误吗? 下面是供参考的试块:
context "with valid inputs" do
let(:alice) { Fabricate(:user) }
let(:folder) { Fabricate.attributes_for(:folder) }
before do
login_user(alice)
post :create, folder: folder
end
it "redirects to the folder show page" do
response.should redirect_to folder_path(folder)
end
以及对应的控制器代码:
def create
new_folder(folder_params)
if @folder.save
flash[:success] = "Folder Created"
redirect_to folder_path(@folder)
else
flash[:danger] = "An Error occured."
render :new
end
end
如评论中所述,Fabricate.attributes_for
不是创建模型实例,而是创建模型所有属性的散列(不包括 id 属性)。因此,当您将文件夹传递给 folder_path
时,rails 正在寻找哈希的 ID。
这是一种测试重定向的方法:
it "redirects to the folder show page" do
response.should redirect_to folder_path(Folder.last)
end
还要确保最后一个文件夹是您想要的文件夹:
it "creates a folder" do
Folder.last.attributes.except(:id).each do |key, value|
folder[key].should eq(value)
end
end