私はちょっと怪しいです。私は、次の統合テストを持っている:RspecとRack :: TestによるREST-API応答のテスト
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私を見るようだ。
私はlast_response
が離れて行くと、私が代わりにresponse
を使用することができますし、私は現在の応答を取得ラック::テスト::メソッドを削除した場合。すべてがうまくいくようです。
これはなぜですか? response
メソッドはどこから来ますか?セッションから前の応答を取得するのにresponse
を使用できますか?
私はルートを照合できるように
last_response.headers["Location"].should eql(route)
ため、last_response
、または類似のものを使用する必要があります。それがなければ、私は設定されるだろう。
'login'のコードは何ですか?それはrspecで定義されたメソッドですか、それとも他のどこかで定義しましたか?ログインページにリダイレクトが必要ですか?もしそうなら、ログインコードのあとに 'follow_redirect!'を書いてください。 – iain
あなたが思いついた解決策は何ですか?私は[Ticketee](https://github.com/rails3book/ticketee)の例でも動作しますが、まだ 'last_response'には私が期待する応答が含まれていませんか?また、答えの1つをマークすることを忘れないでください。 – JJD