我正在尝试在rails应用程序中创建一个测试,但似乎无法找到加载图像的方法并将其发布。我在运行测试时遇到“未初始化的常量ActionController :: TestUploadedFile(NameError)”错误(第5行)。
context "testing user profile uploader" do
let(:user) { FactoryGirl.create(:user, id: 123) }
image = "base/app/assets/images/fallback/default.png"
file = ActionController::TestUploadedFile.new image, "image/png"
post :update, :user => { :profile_image => file }
u = User.find(123)
it "should be able to upload a user's profile image" do
expect(u.profile_image.file).to be_equal "default.png"
end
it "should be able to find profile_avatar for the user's image" do
expect(u.profile_image.versions[:profile_avatar].file).to be_equal "profile_avatar_default.png"
end
it "should be able to find small_avatar for the user's image" do
expect(u.profile_image.versions[:small_avatar].file).to be_equal "small_avatar_default.png"
end
it "should be able to find avatar for the user's image" do
expect(u.profile_image.versions[:avatar].file).to be_equal "avatar_default.png"
end
it "should be able to find large_avatar for the user's image" do
expect(u.profile_image.versions[:large_avatar].file).to be_equal "large_avatar_default.png"
end
end
我该怎么做才能加载图片?对不起,如果代码很糟糕,但我是ruby / rails新手。 谢谢!
更新:所以,我根据你的建议更改了文件。现在唯一的问题是我得到一个“`initialize':base / app / assets / images / fallback / default.png文件不存在(RuntimeError)”,这是有道理的,因为我的图像是不在夹具文件夹中。该应用程序有点旧,因此它没有project / fixtures文件夹。
答案 0 :(得分:1)
你从哪里获得代码? Rails 4中不存在ActionController::TestUploadedFile
。请尝试以下方法:
http://apidock.com/rails/ActionDispatch/TestProcess/fixture_file_upload
答案 1 :(得分:0)
TestUploadedFile在Rails 4中移动。您可以在Rack::Test::UploadedFile
找到它。