balanced / balanced-ruby

Balanced API library in ruby.
MIT License
111 stars 47 forks source link

Best practice for stubbing network calls in tests? #152

Closed dylanjha closed 10 years ago

adambarthelson commented 10 years ago

For finding customers I've just used:

stub_request(:get,"https://<your secret key>:@api.balancedpayments.com/v1/customers?email=#<test email>").
    with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'balanced-ruby/0.7.4'}).
       to_return(:status => 200, :body => "", :headers => {})

You can stick these in a method to and just pass it emails or parameters that you need for the url, keeping your tests a little cleaner.

dylanjha commented 10 years ago

you're using this library? https://github.com/bblimke/webmock

adambarthelson commented 10 years ago

Correct, if that works for your case I'd probably close the issue. I'd expect they'd prefer to see this on stackoverflow than in open issues.

mahmoudimus commented 10 years ago

@adambarthelson @dylanjha yeah there's a stackoverflow issue, http://stackoverflow.com/questions/11890336/whats-the-best-way-to-stub-out-network-calls-with-faraday-in-the-balanced-ruby/13021694#13021694

Does this help?

dylanjha commented 10 years ago

@mahmoudimus thank you, I saw that question. that answer helped but I wasn't sure if there were some other ways/alternatives. I've add this as an answer to that stack overflow question of yours. Thanks.