Project

rspec-hal

0.01
No commit activity in last 3 years
No release in over 3 years
Matchers and helpers for specing HAL documents.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
~> 10.1

Runtime

< 4.0.0.a, >= 2.0
< 4.0.0.a, >= 2.0
 Project Readme

Build Status Code Climate

Rspec Hal

Provides matchers and convenience methods for verifying HAL documents.

Usage

Given the following string stored in a_user_doc.

 {
   "name": "Alice",
   "hobbies": [{"name": "Basketball", "type": "sport"},
               {"name": "Basket weaving", "type": "craft"}],
   "_links": {
     "self": { "href": "http://example.com/alice" },
     "knows": [{ "href": "http://example.com/bob" },
               { "href": "http://example.com/jane" }],
     "checkBusy": { "href": "http://example.com/is_alice_busy{?at}",
                    "templated": true }
   }
 }

Rspec Hal allows very expressive validation of documents.

    expect(a_user_doc).to be_hal

    expect(a_user_doc).not_to be_hal_collection

    expect(a_user_doc).to have_property "name"
    expect(a_user_doc).to have_property 'name', eq("Alice")
    expect(a_user_doc).to have_property :name, matching(/ice$/)
    expect(a_user_doc).to have_property 'hobbies', including(a_hash_including('type' => 'sport'))

    expect(a_user_doc).to have_relation "knows"
    expect(a_user_doc).to have_relation "knows", eq("http://example.com/jane")

    expect(a_user_doc).to have_templated_relation "checkBusy"
    expect(a_user_doc).to have_templated_relation "checkBusy", with_variable("at")
    expect(a_user_doc).to have_templated_relation("checkBusy").with_variables("at")

    expect(parse_hal(users_collection_doc).first).to have_property "name"

Any matcher (actually anything that responds to #===) can be passed as the second argument and will be used to verify the property value or the link href.

be_hal_collection checks that the document is both a valid HAL document and is a page of an RFC 6573 collection

be_hal checks that the document is both a valid HAL document.

Installation

Add this line to your application's Gemfile:

gem 'rspec-hal'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-hal

Then include the matchers by adding this to your spec_helper

RSpec.configuration.include RSpec::Hal::Matchers
RSpec.configuration.include RSpec::Hal::Helpers

(Don't forget to require "rspec-hal" if you are not using bundler.)

If you want to only include the matchers for certain type of specs (say, view specs for example)

RSpec.configuration.include RSpec::Hal::Matchers, type: 'view'
RSpec.configuration.include RSpec::Hal::Helpers, type: 'view'

Contributing

  1. Fork it ( http://github.com/pezra/rspec-hal/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes and lib/rspec/hal/version.rb following semver rules
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request