在api控制器规范中附加请求参数

时间:2014-07-11 19:23:30

标签: ruby-on-rails-4 rspec rspec-rails rails-api doorkeeper

我正在以下列格式向我的API发送请求

http://server/url?v=HEAD&access_token=TOKEN

我的问题是如何在控制器规范中的请求中附加 v 参数和 access_token 参数?

以下是我目前的样本。它都不起作用:

describe Api::ShopsController do
  let(:application) { Doorkeeper::Application.create(:name => "MyApp", :redirect_uri => "http://app.com") }
  let(:user)        { FactoryGirl.create(:user) }
  let(:token)       { Doorkeeper::AccessToken.create! :application_id => application.id, :resource_owner_id => user.id }

  describe "GET #index" do
    it "should respond with 200" do
      get :index, :format => :json, :access_token => token.token
      response.status.should eq(200)
    end

    it "should list all shops" do
      Shop.should_receive(:all) { [] }
      get :index, :format => :json, :access_token => token.token
      response.body.should == [].to_json
    end
  end

  # Example url: localhost:3000/shops/1?v=HEAD&access_token=TOKEN
  describe "GET show" do
    it 'responds with 200' do
      shop = Shop.create! FactoryGirl.attributes_for :shop
      get :show, {id: shop.id, :access_token => access_token.token, v: "HEAD"}, :format => :json 
      response.status.should eq(200)
    end

    it "returns the requested shop" do
      shop = Shop.create! FactoryGirl.attributes_for :shop
      get :show, {:id => user.to_param}, :format => :json, :access_token => token.token
      assigns(:user).should eq(user)
  end
end

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

get shop_path(id: shop.id, v: 'HEAD', access_token: token.token, format: :json)