Search code examples
ruby-on-rails-4rspecrspec-railsrails-apidoorkeeper

appending parameters to request in an api controller spec


I am sending requests to my API in the following format

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

My question is how do I append the v parameter and access_token parameter in my requests within the controller specs?

Below is a sample of what I have so far. None of it works:

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

Solution

  • did you try this?

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