rspec_api_documentation icon indicating copy to clipboard operation
rspec_api_documentation copied to clipboard

Boolean attribute becomes a string when doing_request

Open stephanebruckert opened this issue 8 years ago • 2 comments

Hi,

In the following do_request, terms becomes a string "true" instead of staying a boolean true.

it "creates new user" do
  user = build(:user)
  do_request(
    username: user.username,
    email: user.email,
    password: user.password,
    password_confirmation: user.password,
    terms: true)
  expect(status).to eq(200)
  @data = JSON.parse(response_body)
  expect(@data["status"]).to include("success")
  expect(@data["data"]["email"]).to include(user.email)
end

which means that just for my tests, I have to do the following in my model:

validates_acceptance_of :terms, allow_nil: false, on: [:create], accept: [true, "true"]
                                                                                   ^

Do you know if this expected or a bug? Thank you

stephanebruckert avatar Jul 11 '17 11:07 stephanebruckert

I think this is a bug. You should be able to pass through as: :json but it doesn't seem to work. I am getting the same issue with integers turning into strings.

See: https://github.com/rspec/rspec-rails/issues/610

multiplegeorges avatar Dec 11 '18 17:12 multiplegeorges

This just needs better set up to work as you are expecting it to. do_request will send as a post body which will convert everything to strings. If you're sending JSON data you need to alter the raw_post variable via a let(:raw_post) { ... }.

oestrich avatar Dec 11 '18 17:12 oestrich