ruby on rails - How can I test Stripe.js using poltergeist and Capybara? -


i've been going nuts trying write automated test user sign page. users charged recurring subscription via stripe. input basic details (email, password, etc) , credit card details on same form, following flow happens:

  1. (on client-side) stripe.js makes ajax request stripe's servers, (assuming valid) returns credit card token.
  2. my javascript fills in hidden input in html form credit card token, , submits form rails server.
  3. (now on server-side): validate user's basic details. if they're invalid, return (because there's no point charging them via stripe if e.g. email address invalid can't create account anyway.)
  4. if they're valid, attempt create stripe::customer object, add right subscription , charge them using stripe's ruby gem etc.

all of works fine... except can't figure out how test it. testing step #4 easy enough takes place on server-side can mock out stripe calls gem vcr.

step #1 what's giving me trouble. i've tried test using both puffing-billy , stripe-ruby-mock gem, nothing works. here's own javascript (simplified):

    var striperesponsehandler = function (status, response) {       console.log("response handler called");       if (response.error) {         // show errors on form       } else {         // insert token form gets submitted server         $("#credit_card_token").val(response.id);          // submit form.         $form.get(0).submit();       }     }       $form.submit(function (event) {       // disable submit button prevent repeated clicks       $submitbtn.prop("disabled", true);       event.preventdefault();        console.log("creating token...");       stripe.createtoken(         // credit card details form         // , input them here.       }, striperesponsehandler);        // prevent form submitting normal way.       return false;     }); 

just reiterate, works fine when test manually. automated tests fail:

 failure/error: expect{submit_form}.to change{user.count}.by(1)    expected result have changed 1, changed 0 

when try use gem puffing-billy, seems caching stripe.js (which loaded stripe's own servers @ js.stripe.com, not served own app, stripe don't support this.), call initiated stripe.createtoken isn't being cached. in fact, when log stripe server logs, doesn't seem call been made (or @ least stripe isn't receiving it.)

note console.log statements in js above. when run test suite, line "creating token..." gets printed, "response handler called." doesn't. looks response handler never being called.

i've left out details because question long, can add more on request. doing wrong here? how can test sign page?

update see [my comment on github issue] on stripe-ruby-mock more info on i've tried , failed.

if understand correctly...

capybara won't know ajax requests. should able stub out ajax requests sinatra. have return fixtures same vcr.

here's article on it.

https://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http

you need boot sinatra app in capybara , match urls in ajax calls.

something like:

class fakecontinousintegration < sinatra::base   def self.boot     instance = new     capybara::server.new(instance).tap { |server| server.boot }   end    '/some/ajax'     # send ajax capybara   end end 

when boot server, return address , port can write config js can use.

@server = app.boot 

then use address , port config js app

def write_js_config   config['api'] = "http://#{@server.host}:#{@server.port}"   config.to_json end 

in spec_helper.rb send in config js script points sinatra app. mine compiles gulp. build config before tests run:

system('gulp build --env capybara') 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -