使用 Rspec 和 Rack::Test 测试 REST-API 响应 [英] Testing REST-API responses with Rspec and Rack::Test

查看:21
本文介绍了使用 Rspec 和 Rack::Test 测试 REST-API 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点不知所措.我有以下集成测试:

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

我添加了所需的 Rack::Test::Methods 来获取 last_response 方法,但它似乎无法正常工作.last_response 似乎总是向我显示登录页面,即使我已经登录.

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.

如果我删除 Rack::Test::Methods last_response 消失,我可以使用 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.

这是为什么?response 方法从何而来?我可以使用 response 从会话中获取上一个响应吗?

Why is this? Where is the response method coming from? Can I use response to get the previous response from the session?

我需要为

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 对于某些规范类型是自动的.

response is automatic for certain spec types.

Rspec 可能混合了 ActionController::TestCase::Behavior 用于 :type =>:api 块.
response 将来自 ActionController::TestCase::Behavior,就像 :type =>:controller 块.

Rspec probably mixes ActionController::TestCase::Behavior for :type => :api blocks.
response would come from ActionController::TestCase::Behavior, as it does for :type => :controller blocks.

如果您想在 response 给出的响应之前获得响应,请在发出下一个请求之前尝试将其存储在变量中.

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-rails/v/2-3/docs/controller-specshttps://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.

这篇关于使用 Rspec 和 Rack::Test 测试 REST-API 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆