简体   繁体   English

Rails / RSpec:如何使用第三方API测试Rails动作?

[英]Rails / RSpec: How to test Rails action with third party API?

class SessionsController < ApplicationController::Base  
  def create
      result = verify_sms_code(params[:session][:phone], params[:session][:code]
       if result['code'] == 200
         render json: {code: 200, msg: 'success'}
       else
         render json: {code: 404, msg: 'failed'}
       end
    end
end

The verify_sms_code method invokes third party API from network, the API returns JSON result for validation short message. verify_sms_code方法从网络调用第三方API,该API返回JSON结果以进行验证。

How can I test create action? 如何测试创建动作? Thanks 谢谢

You can stub method making external api call ie verify_sms_code in your case. 您可以存根方法进行外部api调用,即您的情况下的verify_sms_code。 You can write something like 你可以写类似

SessionsController.any_instance.stub(:verify_sms_code).and_return({code: 200, data: ''})

When you write above code whenever you use verify_sms_code inside SessionsController it will return the hash that you provide ie {code: 200, data: ''} 当您在SessionsController内使用verify_sms_code编写上述代码时,它将返回您提供的哈希,即{code: 200, data: ''}

If you are using rspec 3 and above, you can mock verify_sms_code as following inside controller. 如果您使用的是rspec 3及更高版本,则可以在控制器内部按以下方式模拟verify_sms_code

allow(controller).to receive(:verify_sms_code).and_return({code: 200, data: ''})

Reference link: 参考链接:

https://www.relishapp.com/rspec/rspec-mocks/v/2-6/docs/method-stubs/stub-on-any-instance-of-a-class https://www.relishapp.com/rspec/rspec-mocks/v/2-6/docs/method-stubs/stub-on-any-instance-of-a-class

http://rspec.info/documentation/3.4/rspec-mocks/ http://rspec.info/documentation/3.4/rspec-mocks/

There are also other ways to mock. 还有其他模拟方法。 Check below link 检查以下链接

https://robots.thoughtbot.com/how-to-stub-external-services-in-tests https://robots.thoughtbot.com/how-to-stub-external-services-in-tests

Example: 例:

it "does something" do   
  SessionsController.any_instance.stub(:verify_sms_code).and_return({code: 200, data: "success data"})
end

it "does other thing" do 
   SessionsController.any_instance.stub(:verify_sms_code).and_return({code: 404, data: "error data"})
end

I use the VCR gem for testing actions that make API calls. 我将VCR gem用于测试进行API调用的操作。 It lets you record API responses to use them in tests. 它使您可以记录API响应以在测试中使用它们。

https://github.com/vcr/vcr https://github.com/vcr/vcr

I use webmock and like it quite a lot. 我使用webmock ,非常喜欢它。 I get a lot of control over headers, params, return content, return headers, return status, etc. But, not a lot of overhead. 我对标题,参数,返回内容,返回标题,返回状态等有很多控制。但是,开销并不大。

I put all of my stub_requests into a shared set of files then include them en masse via rails_helper.rb . 我将所有stub_requests放入一组共享文件中,然后通过rails_helper.rb包括rails_helper.rb That way, I can reuse my stub_requests throughout my test suite. 这样,我可以在整个测试套件中重用stub_requests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM