Ruby on rails Rack::Test rspec Test中的last_response.body为空(但在实际应用程序中不是)
我已经为此挣扎了几个小时了。我正在使用Rack::Test为我的Rails 3.2应用程序编写API测试 不管我做什么,最后一个_响应的主体是空的(具体来说,它有“{}”,所以有2个字符) 以下是测试:Ruby on rails Rack::Test rspec Test中的last_response.body为空(但在实际应用程序中不是),ruby-on-rails,Ruby On Rails,我已经为此挣扎了几个小时了。我正在使用Rack::Test为我的Rails 3.2应用程序编写API测试 不管我做什么,最后一个_响应的主体是空的(具体来说,它有“{}”,所以有2个字符) 以下是测试: describe "updating a product set with JSON" do def app ProductSetsController.action(:update) end let(:update_json) { ... } before do
describe "updating a product set with JSON" do
def app
ProductSetsController.action(:update)
end
let(:update_json) { ... }
before do
@product_set = FactoryGirl.build(:product_set)
end
it { @failures.should == 0 }
it "should not increment the product set count" do
expect { put :update, update_json }.to_not change(ProductSet, :count).by(1)
end
it "should increment the conditions count" do
expect { put :update, update_json }.to change(@product_set.conditions, :count).by(2)
end
context "response should be valid" do
before do
put :update, update_json
end
subject { last_response }
it { should be_ok }
end
end
所有这些测试都通过了。但是身体是空的
奇怪的是,如果我运行实际的应用程序,响应体肯定不是空的。它包含关于更新产品集的JSON
所以我设置的测试有点不正确。我打赌我忽略了一些非常愚蠢的事情,因为这是我第一次使用Rack::Test。有什么想法吗
我只是觉得我可能没有正确设置请求头。我也在使用RABL生成
更新:
问题确实出在拉布尔身上。尚未找到解决方案,但如果我使用:
respond_with(@product_set.update_attributes(get_properties(params)),
file: 'app/views/product_sets/update.json.rabl', :handlers => [:rabl])
而不是:
respond_with(@product_set.update_attributes(get_properties(params)))
然后它在测试中工作,而两者都在开发或生产中工作。我还确认了这不是Gemfile问题。找到了答案。简言之,确保在rspec文档中根描述块顶部使用关键字“render_views”,以确保正确呈现视图 这是一篇很有帮助的文章: