ruby-on-rails – 如何使用poltergeist和Capybara测试Stripe.js
我一直在努力为我的用户注册页面写一个自动测试.用户将通过条纹向您收取定期订阅.他们在相同的表单上输入他们的基本信息(电子邮件,密码等)和信用卡详细信息,然后发生以下流程:
>(在客户端)stripe.js对Stripe的服务器发出一个AJAX请求,(假设一切都有效)返回信用卡令牌. 所有这一切都非常好,除了我不知道如何测试.测试步骤4很容易,因为它发生在服务器端,所以我可以用像VCR这样的宝石来嘲笑Stripe调用. 第一步是什么让我烦恼我尝试使用puffing-billy和条纹ruby-mock宝石来测试,但没有任何效果.这是我自己的javascript(简体): var stripeResponseHandler = function (status,response) { console.log("response handler called"); if (response.error) { // show the errors on the form } else { // insert the token into the form so it gets submitted to the server $("#credit_card_token").val(response.id); // Now submit the form. $form.get(0).submit(); } } $form.submit(function (event) { // Disable the submit button to prevent repeated clicks $submitBtn.prop("disabled",true); event.preventDefault(); console.log("creating token..."); Stripe.createToken( // Get the credit card details from the form // and input them here. },stripeResponseHandler); // Prevent the form from submitting the normal way. return false; }); 只是重申一下,当我手动测试时,这一切都可以正常工作.但是我的自动测试失败: Failure/Error: expect{submit_form}.to change{User.count}.by(1) expected result to have changed by 1,but was changed by 0 当我尝试使用gem puffing-billy,它似乎是缓存stripe.js本身(它是从Stripe自己的服务器在js.stripe.com加载,不是从我自己的应用程序提供,因为Stripe不支持这一点. ),但是由Stripe.createToken发起的呼叫没有被缓存.实际上,当我登录到我的Stripe服务器日志时,似乎没有打电话(至少Stripe没有收到). 注意我的JS中的console.log语句.当我运行我的测试套件时,打印出“创建令牌…”行,但是调用“响应处理程序”.没有.看起来响应处理程序从来没有被调用. 我已经省略了一些细节,因为这个问题已经很长了,但可以根据要求增加更多.我在这里做错了什么?如何测试我的注册页面? 更新请参阅条纹ruby-mock上的[关于此Github问题的评论]以获取有关我尝试和失败的更多信息. 解决方法
如果我明白了…
Capybara不知道你的ajax请求.您应该能够使用Sinatra来存储AJAX请求.让它返回一个与录像机大致相同的灯具. 这是一篇文章. https://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http 您需要在Capybara启动Sinatra应用程序,然后匹配您的ajax调用中的URL. 就像是: class FakeContinousIntegration < Sinatra::Base def self.boot instance = new Capybara::Server.new(instance).tap { |server| server.boot } end get '/some/ajax' # send ajax back to capybara end end 当您启动服务器时,它会将您可以写入的地址和端口返回给您的js可以使用的配置. @server = App.boot 然后我使用地址和端口来配置JS应用程序 def write_js_config config['api'] = "http://#{@server.host}:#{@server.port}" config.to_json end 在spec_helper.rb中将配置发送到js,以便您的脚本指向您的sinatra应用程序.我们用gulp编译.所以我在构建测试运行之前,将config设置为: system('gulp build --env capybara') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |