测试REST的API响应使用RSpec和机架::测试 [英] Testing REST-API responses with Rspec and Rack::Test
问题描述
我有点难倒。我有以下的集成测试:
I am a bit stumped. I have the following integration test:
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
我添加所需的机架::测试::方法让我在 last_response
方法,但它似乎没有工作的权利。 last_response
似乎总是告诉我sign_in页即使我已经登录。
I've added the required Rack::Test::Methods to get me the last_response
method but it doesn't seem to work right. last_response
always seems to show me sign_in page even though I've already signed in.
如果我删除机架::测试::方法 last_response
消失了,我可以使用响应
而不是和我得到电流响应。一切似乎都工作正常。
If I remove Rack::Test::Methods last_response
goes away and I can use response
instead and I get the current response. Everything seems to works ok.
这是为什么?哪里响应
方法是从哪里来的?我可以用响应
来获取会话中的previous反应?
Why is this? Where is the response
method coming from? Can I use response
to get the previous response from the session?
我需要使用 last_response
,或者类似的东西,为
I need to use the last_response
, or something similar, for
last_response.headers["Location"].should eql(route)
这样我可以匹配的路由。如果不是为了这个,我会被设置。
so that I can match the routes. If it weren't for this I would be set.
推荐答案
响应
是自动的对某些类型的规范
response
is automatic for certain spec types.
Rspec的可能混合的ActionController :: TestCase的行为::
为:类型=> :API
块结果。响应
将来自的ActionController :: TestCase的行为::
,因为它为:类型= GT; :控制器
块
Rspec probably mixes ActionController::TestCase::Behavior
for :type => :api
blocks.
response
would come from ActionController::TestCase::Behavior
, as it does for :type => :controller
blocks.
如果你想获得在这之前给出的回应响应
,尝试存储在一个变量,你做下一个请求之前。
If you want to get the response before that given by response
, try storing it in a variable before you make the next request.
HTTPS://www.relishapp。 COM / RSpec的/ RSpec的护栏/ v / 2-3 /文档/控制器规格和 HTTPS ://github.com/rspec/rspec-rails 的给关于什么是与一些不同的规范类型的混合中的某些信息
https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/controller-specs and https://github.com/rspec/rspec-rails give some information about what is mixed in with some of the various spec types.
这篇关于测试REST的API响应使用RSpec和机架::测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!