/var/log/messages

Jan 21, 2014 - 1 minute read - Comments - rails

Rails API 関連の備忘など

Rails API Testing Best Practice with RSpec を見つつ試験修正。

とりあえず JSON Helper 追加。spec/supprt/request_helper.rb に以下を、なのか。

module Requests
  module JsonHelpers
    def json
      @json ||= JSON.parse(response.body)
    end
  end
end

で、spec/spec_helper.rb に以下を追加とのこと。

RSpec.configure do |config|
  config.include Requests::JsonHelpers, type: :request
end

最初と最後はスデに記述済み、が前提です

これで JSON.parse な部分を

      get path
      res = JSON.parse(response.body)
      expect(res['hoge']).to eq('hoge')

以下のように書けるよね、という事な模様。

      get path
      expect(json['hoge']).to eq('hoge')

あとはバージョン云々な以下のドキュメントを備忘、ってことで。