7

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
Msencenb
  • 5,675
  • 11
  • 52
  • 84

2 Answers2

1

The only thing that looks incorrect to me here is .stubs when it should be .stub.

stephhippo
  • 104
  • 4
  • 1
    Thanks for the response, I've added the full test method to the question. Note that changing it to .stub still has the same error being thrown. Part of the problem is that I don't want to use rspec, this is a standard out of the box rails integration test. – Msencenb Jun 23 '15 at 05:53
1

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})
Matt Brictson
  • 10,904
  • 1
  • 38
  • 43