我使用grape在我的rails应用程序中创建了一个api。
现在我想测试我所有的api。在我的api中,我可以上传声音,获取声音/:id和删除声音/:id。
我开始为post声音编写测试,但是我的测试总是返回"undefined method [] for nil:NilClass“。
我的curl命令用于上传(这就是工作):
curl -X POST -i -F data=@SOUND/test.mp3 "http://localhost:3000/api/sounds/new?access_token=1234567"
我认为问题出在curl中当你使用"-F data=“时,你需要在"=”后面加上@,如果我去掉@,我有一个未定义的方法[]来表示nil:NilClass错误。
现在我的问题是如何在我的测试中添加@ for测试上传文件?
我的测试:
it "uploads a file" do
sound_filename = "spec/fixtures/test.mp3"
post "/api/sounds/new", data= Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)
expect(response.status).to eq(201)
expect(response.headers['Content-Type']).to eq("audio/mp3")
expect(response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''test.mp3")
end
对于nil:NilClass错误,此测试返回此错误: undefined method []。有人知道我怎么解决它吗?
谢谢你的帮助。
发布于 2015-09-30 16:10:49
把这一行替换掉怎么样
post "/api/sounds/new", data= Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)
使用
post "/api/sounds/new", {data: Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)}
https://stackoverflow.com/questions/25805805
复制相似问题