These are chat archives for rebelidealist/stripe-ruby-mock

11th
Nov 2015
kathyonu
@kathyonu
Nov 11 2015 06:35
@mezerny Hi .. i have been offline for 5 days .. I am amazed to see no one answers you in all this time !! So, I will try to help .. can you share the code you are using to test with here, or in a Gist ?
mezerny
@mezerny
Nov 11 2015 09:57
require 'rails_helper'
require 'stripe_mock'

describe SubscriptionsController do
  let(:stripe_helper) { StripeMock.create_test_helper }

before(:all) do
    Stripe.api_key = 'apikey'
    StripeMock.start
  end

  after(:all) { StripeMock.stop }

describe 'testing webhook endpoint',  live: true do
       it 'returns 200 OK', live: true do
      date =  DateTime.now - 1.month
      practice = Practice.find_or_create_by!(
          {
              name: 'Test practice',
              practice_type_id: 6,
              salutation: 'Dr.',
              subscription_active_until: date, #in the past
              trial_period: false,
              stripe_customer_id: 'cus_00000000000000'
          })
      event = StripeMock.mock_webhook_event('invoice.payment_succeeded')
      post :prolong_subscription,  event.to_json
      expect(response).to have_http_status(:ok)

    end
  end
end
@kathyonu thanks for your reply, here is my test code
kathyonu
@kathyonu
Nov 11 2015 15:59
@mezerny Most welcome. First thing I suggest is place your stripe_test_keys into your environment variables and draw from there, so that the keys you need are in all tests automatically. If you have any question on that statement, see Rails Environment Variables, by Taylor Mock and @DanielKehoe
@mezerny Second thing is you do not want to use the live: true twice in the same describe block. You can do one or the other, but both are not required and might be the cause of error.. Speaking of which, please show the response you are getting when you run this test, thank you.
I will be editing your code and posting momentarily. I will be including the usage of Warden to reset the system after the test is done.
kathyonu
@kathyonu
Nov 11 2015 17:21

@mezerny

require 'stripe_mock'
require 'stripe_mock/server'
include Warden::Test::Helpers
Warden.test_mode!

RSpec.configure do
  def gen_card_tk
    stripe_helper.generate_card_token(last4: '4242')
  end
end

describe 'SubscriptionsController', live: true do
  let(:stripe_helper) { StripeMock.create_test_helper }

  before(:each) do
    StripeMock.start
  end

  after(:each) do
    StripeMock.stop
    Warden.test_reset!
  end

  context 'testing webhook endpoint' do
    it 'returns 200 OK' do
      date =  DateTime.now - 1.month
      practice = Practice.find_or_create_by!(
        name: 'Test practice',
        practice_type_id: 6,
        salutation: 'Dr.',
        subscription_active_until: date, # in the past
        trial_period: false,
        stripe_customer_id: 'cus_00000000000000'
      )
      event = StripeMock.mock_webhook_event('invoice.payment_succeeded')
      post :prolong_subscription, event.to_json
      expect(response).to have_http_status(:ok)
    end
  end
end

Please run this code and show any error here. I run the code, and because I do not have Practice as an Object, this is my failure:

SubscriptionsController testing webhook endpoint returns 200 OK
     Failure/Error: practice = Practice.find_or_create_by!(
     NameError:
       uninitialized constant Practice

What is the response to your running the above edited test? Also, you can comment out the Warden calls if you are not using it.

kathyonu
@kathyonu
Nov 11 2015 17:53
@mezerny Remember to add your stripe api test key in the manner you do, and here is a Stripe reference for you: Responding to a Webhook
mezerny
@mezerny
Nov 11 2015 19:21
@kathyonu Should I define StripeMock.toggle_live(true) inside RSpec.configure block?
kathyonu
@kathyonu
Nov 11 2015 19:23
@mezerny I never use it so my answer is no. I believe the usage of that command is: inside a test where the live is already true, and you want to stop it.
mezerny
@mezerny
Nov 11 2015 19:35
Well, after adding require 'rails_helper' and Stripe.api_key into your code I was able to run test successfully, but it doesn't hit Stripe's servers for a live webook event
mezerny
@mezerny
Nov 11 2015 19:49
@kathyonu regarding responding to a webhook, my controller does exactly as described in Stripe's documentation - it returns 200 OK status code when webhook event was handled successfully
@kathyonu The problem is that I can run tests in offline mode, but can't get them into 'live' mode as described here: stripe-ruby-mock: Live Testing
kathyonu
@kathyonu
Nov 11 2015 22:25

@mezerny Does your rails_helper.rb file look like this:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

require 'capybara/rails'
require 'capybara/rspec'
require 'rspec/mocks'
require 'stripe_mock'
require 'stripe_mock/server'
require 'thin'
ARGV = [] # Reset ARGV so Dante will quit using rspec params
StripeMock.spawn_server # Live tests, run $ rspec -t live
ActiveRecord::Base.connection.reconnect!
...

I have had to add those last three lines to run live tests.

kathyonu
@kathyonu
Nov 11 2015 22:51
@mezerny Please show the Terminal response to your failing test.