Rspec-rails: JSON boolean being converted to strings when testing post call in test env

Created on 24 Nov 2016  Â·  6Comments  Â·  Source: rspec/rspec-rails

I've been testing a JSON request to our API, It will respond with JSON. It seems like all the boolean within the JSON get converted to strings as we post them the endpoint only under test environment.

However RAILS_ENV=development bundle exec guard works ok (no conversion), but once RAILS_ENV=test bundle exec guard converts all the boolean to string, which is unexpected.

This is my test case using mock.

  it 'create proration item' do
    # guess need to create a dummy account with stripe_customer_id
    account = create(:account, stripe_customer_id: 'cus_00000000000000')
    invoice = create(:invoice, stripe_invoice_id: 'in_00000000000000' )

    # create a plan
    plan = create(:plan)
    stripe_helper.create_plan(id: plan.stripe_plan_id)

    Stripe::InvoiceItem.create(
      amount: 300,
      currency: 'jpy',
      description: "#{300} charged",
      proration: true,
      type: 'invoiceitem'
    )
    item_objs = Stripe::InvoiceItem.list(:limit => 10)

    # create a event mock based on objs above
    event = StripeMock.mock_webhook_event('invoice.payment_succeeded', {
      id: invoice.stripe_invoice_id,
      lines: item_objs,
      customer: account.stripe_customer_id
    })

    # Mocking up private method :fetch_invoice_details
    balance_txn = Stripe::BalanceTransaction.retrieve('txn_05RsQX2eZvKYlo2C0FRTGSSA')
    allow_any_instance_of(InvoicePaymentSucceeded).to receive(:fetch_invoice_details).and_return(balance_txn)

    # when it's false (This should be changed depending on cases). Here we don't test private method.
    allow_any_instance_of(InvoicePaymentSucceeded).to receive(:initial_invoice?).and_return(false)

    post '/stripe-events', event.as_json
    expect(response.status).to eq 200
  end

Within end point handler, I could see the values below.

Under **test environment**,

#<Stripe::Event:0x3fe2ea57e95c id=test_evt_2> JSON: {
  "id": "test_evt_2",
  "created": "1326853478",
  "livemode": "false",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "data": {"object":{"id":"in_00000000000000","date":"1394018368","period_start":"1394018368","period_end":"1394018368","lines":{"object":"list","data":[{"id":"test_ii_1","object":"invoiceitem","date":"1349738920","amount":"300","livemode":"false","proration":"true","currency":"jpy","customer":"cus_test","description":"300 charged","invoice":null,"subscription":null,"type":"invoiceitem"}],"url":"/v1/hashs","has_more":"false"},"subtotal":"30000","total":"30000","customer":"cus_00000000000000","object":"invoice","attempted":"true","closed":"true","paid":"true","livemode":"false","attempt_count":"1","amount_due":"0","currency":"usd","starting_balance":"0","ending_balance":"0","next_payment_attempt":null,"charge":"ch_00000000000000","discount":null,"application_fee":null,"subscription":"su_00000000000000","description":null}},
  "controller": "stripe_event/webhook",
  "action": "event"
}

Under **development environment**,

#<Stripe::Event:0x3fce66d141f0 id=test_evt_2> JSON: {
  "id": "test_evt_2",
  "created": 1326853478,
  "livemode": false,
  "type": "invoice.payment_succeeded",
  "object": "event",
  "data": {"object":{"id":"in_00000000000000","date":1394018368,"period_start":1394018368,"period_end":1394018368,"lines":{"object":"list","data":[{"id":"test_ii_1","object":"invoiceitem","date":1349738920,"amount":300,"livemode":false,"proration":true,"currency":"jpy","customer":"cus_test","description":"300 charged","metadata":{},"invoice":null,"subscription":null,"type":"invoiceitem"}],"url":"/v1/hashs","has_more":false},"subtotal":30000,"total":30000,"customer":"cus_00000000000000","object":"invoice","attempted":true,"closed":true,"paid":true,"livemode":false,"attempt_count":1,"amount_due":0,"currency":"usd","starting_balance":0,"ending_balance":0,"next_payment_attempt":null,"charge":"ch_00000000000000","discount":null,"application_fee":null,"subscription":"su_00000000000000","metadata":{},"description":null}}
}

Any ideas about pitfalls in test under test environment?

Most helpful comment

Thanks I think I understood now,
So you mean in my testcases hash is always sent.
And Only under development it's converted into hash, but it does not happen in test environment. That's why I get the error.

All 6 comments

as_json returns a hash, which is being converted to string params for the controller, this is the expected behaviour from Rails (we don't tamper with it), use to_json instead.

@JonRowe I just wonder why it's happened only under test environment. Any ideas?

Because your posting Json not a hash in dev

On Fri, 25 Nov 2016 at 12:55, Toshiki Inami notifications@github.com
wrote:

@JonRowe https://github.com/JonRowe I just wonder why it's happened
only under test environment. Any ideas?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rspec/rspec-rails/issues/1755#issuecomment-262865864,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJ8oFzIiZC0o1j1fZqUeNvrf4q4o4Ruks5rBkAHgaJpZM4K7muV
.

I’m little confused…
Why under only test environment does it post in hash? Is this expected behaviour from Rails?
(Both testcases are exactly the same.)

Values returned from event.as_json are the exactly same under test, and development.

Because your test is sending a hash but a real browser / js sends json

On Fri, 25 Nov 2016 at 14:02, Toshiki Inami notifications@github.com
wrote:

I’m little confused…
Why under only test environment does it post in hash? Is this expected
behaviour from Rails?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rspec/rspec-rails/issues/1755#issuecomment-262872045,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJ8oGL2BlWnFdwoFiun5ifa8AvQ2-DUks5rBk_fgaJpZM4K7muV
.

Thanks I think I understood now,
So you mean in my testcases hash is always sent.
And Only under development it's converted into hash, but it does not happen in test environment. That's why I get the error.

Was this page helpful?
0 / 5 - 0 ratings