【问题标题】:appending parameters to request in an api controller spec将参数附加到 api 控制器规范中的请求
【发布时间】:2014-07-11 19:23:29
【问题描述】:

我正在以以下格式向我的 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

【问题讨论】:

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


    【解决方案1】:

    你试过了吗?

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

    【讨论】:

    • 是的,但我发现了问题。我正在向as_json 方法添加一个额外的额外参数,但未能像@shop.as_json(params[:v]) 那样将其传递给我的控制器中的请求对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多