在我的rails应用程序中,我正在尝试进行基本测试,测试在创建文件夹后,用户被重定向到文件夹显示页面。我已经实现了这个代码,当我通过浏览器完成所有操作时它会起作用,但测试失败并给我这个错误:
Failure/Error: response.should redirect_to folder_path(folder)
ArgumentError:
comparison of Array with Array failed
我在pry中打开了这个测试块,这也是它所说的:
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
答案 0 :(得分:1)
如评论中所述,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