我有点难住了。我有以下集成测试:
require "spec_helper"
describe "/foods", :type => :api do
include Rack::Test::Methods
let(:current_user) { create_user! }
let(:host) { "http://www.example.com" }
before do
login(current_user)
@food = FactoryGirl.create_list(:food, 10, :user => current_user)
end
context "viewing all foods owned by user" do
it "as JSON" do
get "/foods", :format => :json
foods_json = current_user.foods.to_json
last_response.body.should eql(foods_json)
last_response.status.should eql(200)
foods = JSON.parse(response.body)
foods.any? do |f|
f["food"]["user_id"] == current_user.id
end.should be_true
foods.any? do |f|
f["food"]["user_id"] != current_user.id
end.should be_false
end
end
context "creating a food item" do
it "returns successful JSON" do
food_item = FactoryGirl.create(:food, :user => current_user)
post "/foods.json", :food => food_item
food = current_user.foods.find_by_id(food_item["id"])
route = "#{host}/foods/#{food.id}"
last_response.status.should eql(201)
last_response.headers["Location"].should eql(route)
last_response.body.should eql(food.to_json)
end
end
end
我已经添加了所需的 Rack::Test::Methods 来获取last_response
方法,但似乎效果不太好。last_response
即使我已经登录,似乎总是显示登录页面。
如果我删除 Rack::Test::Methodslast_response
消失了,我可以使用response
相反,我得到了当前的响应。一切似乎都正常。
为什么是这样?哪儿是response
方法从何而来?我可以用吗response
从会话中获取先前的响应?
我需要使用last_response
,或类似的东西,对于
last_response.headers["Location"].should eql(route)
这样我就可以匹配路线。如果不是因为这个,我就已经定了。
response
对于某些规格类型是自动的。
Rspec 可能混合ActionController::TestCase::Behavior
for :type => :api
blocks.
response
会来自ActionController::TestCase::Behavior
,就像它的作用一样:type => :controller
blocks.
如果您想在给出之前得到回复response
,在发出下一个请求之前尝试将其存储在变量中。
https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/controller-specs https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/controller-specs and https://github.com/rspec/rspec-rails https://github.com/rspec/rspec-rails提供一些有关与某些不同规格类型混合的内容的信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)