Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stub subscritption status with mocha? #849

Closed
lefterisnik opened this issue Sep 10, 2019 · 4 comments
Closed

How to stub subscritption status with mocha? #849

lefterisnik opened this issue Sep 10, 2019 · 4 comments

Comments

@lefterisnik
Copy link

lefterisnik commented Sep 10, 2019

I am trying to test our migration to SCA and I want to stub the subscription status to incomplete. The code I am using follows:

def test_one
  sign_in(@user)
  Stripe::Subscription.any_instance.stubs(:status).returns("incomplete")
  post do_sign_up_url, params: {
      number_licenses: "2",
      stripe_token_id: @stripe_helper.generate_card_token,
  }, as: :json
  response_body = JSON.parse(response.body)

  assert_response :ok
  assert_equal 1, Event.all.count
end

The backend creates a subscription using the stripe-mock-ruby which currently is not able to handle sophisticated sca workflows, so it returns a subscription with status="active". For that reason, I would like to stub the subscription as above to make sure that my code which checks for extra authentication is working properly. Any help would be appreciate.

@ob-stripe
Copy link
Contributor

Mocking StripeObjects can be complicated due to the fact that all accessor methods are dynamically defined at runtime.

I would recommend using the .construct_from method to create an actual, non-mocked instance instance instead. Something like this:

subscription = Stripe::Subscription.construct_from({id: 'sub_123', object: 'subscription', status: 'incomplete'})

Or if you already have a full subscription object and just want to replace the status, you could do something like this:

# subscription is an existing Stripe::Subscription
incomplete_subscription = Stripe::Subscription.construct_from(subscription.to_h.merge({status: 'incomplete'}))

Does this answer your question?

@lefterisnik
Copy link
Author

Thanks @ob-stripe for the response but unfortunately I don't have access to the subscription object so I can't use the Stripe::Subscription.construct_from(subscription.to_h.merge({status: 'incomplete'})).

@brandur-stripe
Copy link
Contributor

Hey @lefterisnik,

In that case, I'd suggest doing the first option that OB
described above: just create a tiny test fixture and merge
the properties that you care about into that:

subscription = Stripe::Subscription.construct_from({
  id:     'sub_123',
  object: 'subscription'
})
Stripe::Subscription.construct_from(subscription.to_h.merge({status: 'incomplete'}))

Just having the object property present is enough
information for StripeObject to hydrate a Subscription.
It won't have all the normal fields, but it should be
sufficient for testing purposes.

Your other option is to use stripe-ruby-mock to generate a
subscription "fixture" that you could then use elsewhere in
your tests. This code worked for me:

require 'stripe_mock'

Stripe.api_key = 'sk_test_123'

StripeMock.start
stripe_helper = StripeMock.create_test_helper

customer = Stripe::Customer.create(
  currency: "USD",
  source: stripe_helper.generate_card_token,
)
plan = Stripe::Plan.create(
  amount: 1000,
  currency: "USD",
  interval: "month",
  product: {name: "My Plan"},
)
subscription = Stripe::Subscription.create(
  customer: customer.id,
  plan: plan.id
)

p subscription

You can then feed that subscription back into the code from
the top:

Stripe::Subscription.construct_from(subscription.to_h.merge({status: 'incomplete'}))

I'm not sure that I'd completely recommend this last
approach -- I played with stripe-ruby-mock for a few
minutes and found it to be somewhat fiddly. You can get it
working, but until you're passing all the right parameters
it tends to error out in various ways that often bubble out
as things like NoMethodErrors. I'd personally start with
the "simple" fixture of just object/id and see how far
I got with that.

Anyway, we know that the library's testing story isn't
satisfactory. We intend to improve this at some point and
are tracking that feature in #243.

@ob-stripe
Copy link
Contributor

Closing due to age, but feel free to reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants