powerboy
powerboy

Reputation: 10961

In a test, how to pass default request parameters to get/post/put/delete

Consider this example:

describe UsersController do
  it "works for something" do
    # some other code
    get :show, {facebook_id: 1000000000}
    # some other code
    put :update, {facebook_id: 1000000000, birthday: "2001-01-01"}
    # some other code
    get :show, {facebook_id: 1000000000}
    # some other code
  end

  it "works for another thing" do
    # some other code
    get :show, {facebook_id: 1000000000}
    # some other code
  end
end

How to DRY out the {facebook_id: 1000000000} so that I can simple write get :show, put :update, {birthday: "2001-01-01"}, etc.?

Upvotes: 0

Views: 127

Answers (2)

arieljuod
arieljuod

Reputation: 15838

What does your controller do with that facebook_id? I would stub the method that requires that facebook_id when you are not testing that, something like what I suggested here RSpec Request - How to set http authorization header for all requests

Upvotes: 0

oldergod
oldergod

Reputation: 15010

How about

def options(*args)
  _options = args.extract_options!
  _options.merge({facebook_id: 1000000000})
end

then

get :show, options
put :update, options(birthday: "2001-01-01")

Upvotes: 1

Related Questions