Rspec-rails: يتم تحويل JSON المنطقي إلى سلاسل عند اختبار ما بعد المكالمة في بيئة الاختبار

تم إنشاؤها على ٢٤ نوفمبر ٢٠١٦  ·  6تعليقات  ·  مصدر: rspec/rspec-rails

لقد كنت أختبر طلب JSON لواجهة برمجة التطبيقات الخاصة بنا ، وسوف يستجيب مع JSON. يبدو أن كل العناصر المنطقية داخل JSON يتم تحويلها إلى سلاسل بينما نقوم بنشرها في نقطة النهاية فقط في بيئة الاختبار.

ومع ذلك ، فإن RAILS_ENV=development bundle exec guard يعمل بشكل جيد (بدون تحويل) ، ولكن بمجرد تحويل RAILS_ENV=test bundle exec guard كل القيم المنطقية إلى سلسلة ، وهو أمر غير متوقع.

هذه هي حالتي الاختبارية باستخدام الوهم.

  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

ضمن معالج نقطة النهاية ، كان بإمكاني رؤية القيم أدناه.

أقل **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"
}

أقل **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}}
}

أي أفكار حول المزالق في الاختبار في ظل بيئة الاختبار؟

التعليق الأكثر فائدة

شكرا أعتقد أنني فهمت الآن ،
لذلك تقصد أنه يتم دائمًا إرسال التجزئة في حالات الاختبار الخاصة بي.
وفقط أقل development يتم تحويله إلى تجزئة ، لكن لا يحدث ذلك في بيئة test . لهذا السبب حصلت على الخطأ.

ال 6 كومينتر

يُرجع as_json تجزئة ، والتي يتم تحويلها إلى معلمات سلسلة لوحدة التحكم ، هذا هو السلوك المتوقع من ريلز (نحن لا نتلاعب به) ، استخدم to_json بدلاً من ذلك.

JonRowe أتساءل فقط لماذا حدث ذلك فقط في ظل بيئة test . أيه أفكار؟

لأن نشر Json الخاص بك ليس تجزئة في ديف

يوم الجمعة ، 25 نوفمبر 2016 الساعة 12:55 ، Toshiki Inami [email protected]
كتب:

JonRowe https://github.com/JonRowe أنا فقط أتساءل لماذا حدث ذلك
فقط تحت بيئة الاختبار. أيه أفكار؟

-
أنت تتلقى هذا لأنه تم ذكرك.
قم بالرد على هذا البريد الإلكتروني مباشرة ، وقم بعرضه على GitHub
https://github.com/rspec/rspec-rails/issues/1755#issuecomment-262865864 ،
أو كتم الخيط
https://github.com/notifications/unsubscribe-auth/AAJ8oFzIiZC0o1j1fZqUeNvrf4q4o4Ruks5rBkAHgaJpZM4K7muV
.

أنا مرتبك قليلا ...
لماذا في بيئة الاختبار فقط يتم نشرها في التجزئة؟ هل هذا السلوك متوقع من ريلز؟
(كلا حقيبتا الاختبار متطابقتان تمامًا).

القيم التي يتم إرجاعها من event.as_json هي نفسها تمامًا تحت test و development .

لأن اختبارك يرسل تجزئة ولكن متصفح حقيقي / js يرسل json

يوم الجمعة ، 25 نوفمبر 2016 الساعة 14:02 ، Toshiki Inami [email protected]
كتب:

أنا مرتبك قليلا ...
لماذا في بيئة الاختبار فقط يتم نشرها في التجزئة؟ هل هذا متوقع
السلوك من ريلز؟

-
أنت تتلقى هذا لأنه تم ذكرك.
قم بالرد على هذا البريد الإلكتروني مباشرة ، وقم بعرضه على GitHub
https://github.com/rspec/rspec-rails/issues/1755#issuecomment-262872045 ،
أو كتم الخيط
https://github.com/notifications/unsubscribe-auth/AAJ8oGL2BlWnFdwoFiun5ifa8AvQ2-DUks5rBk_fgaJpZM4K7muV
.

شكرا أعتقد أنني فهمت الآن ،
لذلك تقصد أنه يتم دائمًا إرسال التجزئة في حالات الاختبار الخاصة بي.
وفقط أقل development يتم تحويله إلى تجزئة ، لكن لا يحدث ذلك في بيئة test . لهذا السبب حصلت على الخطأ.

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات