{"id":2834,"date":"2015-10-12T15:23:50","date_gmt":"2015-10-12T13:23:50","guid":{"rendered":"https:\/\/sgaul.de\/?p=2834"},"modified":"2015-11-30T21:40:31","modified_gmt":"2015-11-30T20:40:31","slug":"match-api-output-with-yaml-fixture","status":"publish","type":"post","link":"https:\/\/sgaul.de\/2015\/10\/12\/match-api-output-with-yaml-fixture\/","title":{"rendered":"Match API output with YAML fixture"},"content":{"rendered":"

A nice and simple approach to test you JSON APIs:<\/p>\n

# spec\/features\/api\/user_spec.rb\r\n\r\nrequire 'rails_helper'\r\n\r\nRSpec.feature 'User', type: :feature do\r\n  it 'lists users' do\r\n    user = create(:user, :as_admin)\r\n    visit api_user_path(user, format: :json)\r\n    expect(page).to match_yaml_fixture('api\/user')\r\n  end\r\nend<\/pre>\n

This checks if the page source matches the data\u00a0given in the fixture file. To make it more readable we format the expected JSON as YAML:<\/p>\n

# spec\/fixtures\/api\/user.yaml\r\n\r\n---\r\nuser:\r\n- id: 1\r\n  first_name: Max\r\n  last_name: Power\r\n  role:\r\n    id: 1\r\n    name: Admin<\/pre>\n

<\/p>\n

Add\u00a0match_yaml_fixture to RSpec<\/h2>\n

Drop this in your support directory:<\/p>\n

require 'rspec\/expectations'\r\n\r\nmodule FixtureHelper\r\n  def read_fixture(*path_elements)\r\n    IO.read(Rails.root.join(\"spec\", \"fixtures\", *path_elements))\r\n  end\r\nend\r\n\r\nRSpec::Matchers.define :match_yaml_fixture do |expected|\r\n  match do |actual|\r\n    expected = prepare_expected(expected)\r\n    actual = prepare_actual(actual)\r\n    actual.strip == read_fixture(expected).strip\r\n  end\r\n  failure_message do |actual|\r\n    expected = prepare_expected(expected)\r\n    actual = prepare_actual(actual)\r\n    \"expected the following to match fixture file #{expected}:\\n\\n#{actual}\"\r\n  end\r\n\r\n  private\r\n\r\n  def prepare_expected(expected)\r\n    expected += '.yaml' unless expected.end_with?('.yaml')\r\n    expected\r\n  end\r\n\r\n  def prepare_actual(actual)\r\n    actual = JSON.parse(actual.source) if actual.is_a?(Capybara::Session)\r\n    actual = actual.to_yaml if actual.is_a?(Hash)\r\n    actual.to_s\r\n  end\r\nend<\/pre>\n

Then include the helper in your spec_helper.rb or rails_helper.rb\u00a0file:<\/p>\n

# spec\/rails_helper.rb\r\n\r\nrequire File.expand_path('..\/support\/fixture_helper', __FILE__)\r\nRSpec.configure do |config|\r\n\u00a0 config.include FixtureHelper\r\nend<\/pre>\n","protected":false},"excerpt":{"rendered":"

A nice and simple approach to test you JSON APIs: # spec\/features\/api\/user_spec.rb require ‚rails_helper‘ RSpec.feature ‚User‘, type: :feature do it ‚lists users‘ do user = create(:user, :as_admin) visit api_user_path(user, format: :json) expect(page).to match_yaml_fixture(‚api\/user‘) end end This checks if the page source matches the data\u00a0given in the fixture file. To make it more readable we format… Match API output with YAML fixture<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[91],"tags":[553,570,552],"_links":{"self":[{"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/posts\/2834"}],"collection":[{"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/comments?post=2834"}],"version-history":[{"count":1,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/posts\/2834\/revisions"}],"predecessor-version":[{"id":2835,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/posts\/2834\/revisions\/2835"}],"wp:attachment":[{"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/media?parent=2834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/categories?post=2834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sgaul.de\/wp-json\/wp\/v2\/tags?post=2834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}