I'm doing some stripe integration tests and I would like to stub/mock a few endpoints. I'm attempting to do it like this:
Stripe::Charge.stubs(:retrieve).returns({:balance_transaction => 40})
but I'm getting the following:
NoMethodError: undefined method `stubs' for Stripe::Charge:Class
What's the correct syntax for stubbing this? Rails 4, Ruby 2.
Edit: Here is my full test method. Essentially my payment_succeeded webhook hits stripe to retrieve a charge and it's associated balance transaction in order to log the transaction fee. I'm using stripe_mock to mock the webhook events, but I'd rather stub out the rest of them using standard stubbing techniques. Note that even when I change it to 'stub' it throws the same error above (with stub substituted for stubs).
require 'test_helper'
require 'stripe_mock'
class WebhooksTest < ActionDispatch::IntegrationTest
# called before every single test
def setup
StripeMock.start
end
# called after every single test
def teardown
StripeMock.stop
end
test 'invoice.payment_succeeded' do
Stripe::Charge.stubs(:retrieve).returns({:balance_transaction => 40})
event = StripeMock.mock_webhook_event('invoice.payment_succeeded', { :customer => "stripe_customer1", :id => "abc123" })
post '/stripe-events', id: event.id
assert_equal "200", response.code
assert_equal 1, StripeInvoicePayment.count
assert_equal 'abc123', event.data.object.id
end
end
The only thing that looks incorrect to me here is .stubs when it should be .stub.
Since you aren't using RSpec, I would recommend installing the mocha gem to get a full selection of mocking and stubbing tools. Here's a quickstart:
# Gemfile
gem "mocha", :group => :test
# test/test_helper.rb
require "mocha/mini_test"
Now you can stub like this:
Stripe::Charge.stubs(:retrieve => {:balance_transaction => 40})
Or, if you'd like to verify the method was actually called, you could set up an expectation instead:
Stripe::Charge.expects(:retrieve)
.with(:id => "abc123")
.returns({:balance_transaction => 40})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With