【问题标题】:testing Rails json API测试 Rails json API
【发布时间】:2015-09-19 13:06:05
【问题描述】:

我有一个提供 JSON 的 Rails 应用程序,并且想测试一些通过 http 请求和处理一些 json 的代码。在不存根 http 请求-响应的情况下测试此代码的最佳方法是什么?

我正在考虑解雇 webrick 来为 Rails 应用程序提供服务,但在技术上无能为力。任何指向代码示例的指针都值得赞赏。

# lib/bam.rb
require 'open-uri'

class Bam
  def bam host='localhost'
    hash = JSON.parse( open("http://#{host}/bam.json) )
    # process hash
  end
end

# spec/bam_spec.rb
RSpec.describe 'Bam' do
  before(:all) do
    # fire up webrick
  end
  after(:all) do
    # shutdown webrick
  end

  it 'bam' do
    hash = Bam.new.bam
    hash.should eq('bam' => 'bam')
  end
end

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4 json-api


【解决方案1】:

您可以使用空中宝石https://github.com/brooklynDev/airborne。您也可以比较 JSON 结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多