Project

rspec-rabl

0.01
Repository is archived
No release in over 3 years
Low commit activity in last 3 years
Provides a more declarative form of view testing for users of rabl
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
>= 0

Runtime

~> 0.9
~> 3.0
 Project Readme

Build Status Code Climate Dependency Status

⚠️ No Longer Supported

I haven't needed to use this library in a long time and I'm no longer maintaining it to keep it up-to-date with the ruby community. I'm keeping it here as an archive for future reference.

RspecRabl

A collection of convenient helpers for writing view specs with rabl templates.

Update Notes

If you were previously using the rspec_rabl gem please note that you will need to fix a few things in order to upgrade to the latest version:

  • change your Gemfile to require rspec-rabl (a hyphen instead of an underscore see #8 for more details)
  • in spec_helper.rb you need to require rspec/rabl instead of rspec_rabl
  • if you are using rails and you want the default configuration you will need to require rspec/rabl/rails instead of rspec/rails_rabl

Usage

First make sure you configure rspec-rabl in your spec_helper.rb file (see Configuration below).

Now your templates can be rendered with whatever data you like for testing:

describe "budgets/show/rabl" do                            # this tells us what template you want to test
  let(:budget) { Budget.new }
  rabl_data(:root => 'budget') { budget }                  # this tells us what data to use when rendering and what structure you expect the template to have (use root and object_root just like rabl)

  it "renders valid json" do
    expect {
      JSON.parse(rendered_template)                        # rendered_template is the rendered string
    }.not_to raise_error
  end

  it "renders the data in some specific way" do
    expect(parsed_json['budget']['date']).to eq("1984")    # parsed_json is the parsed out version of the rendered string
  end
end

But most of the time you want to check some pretty common things like:

describe "budgets/show.rabl" do
  let(:budget) { Budget.new }
  rabl_data(:root => 'budget') { budget }

  it { expect(subject).to render_attribute(:amount) }                        # parsed_json['budget']['amount'] == budget.amount
  it { expect(subject).to render_attribute(:amount).with(:friendly_amount) } # parsed_json['budget']['amount'] == budget.friendly_amount
  it { expect(subject).to render_attribute(:amount).with_value("45.00") }    # parsed_json['budget']['amount'] == "45.00"
end

describe "budgets/index.rabl" do
  let(:budgets) { [ Budget.new ] }
  rabl_data(:root => 'budgets', :object_root => 'budget') { budgets }

  it { expect(subject).to render_attribute(:amount) }                       # parsed_json['budgets'].first['budget']['amount'] == budgets.first.amount
end

If you don't want to specify the template you are testing via the describe/context string you can specify the template like this:

describe "Users are rendered with the humorous attribute" do
  rabl_template { "users/show.rabl" }
  rabl_data(:root => 'user') { user }

  specify { expect(subject).to render_attribute(:humorous).with_value("not really") }
end

Configuration

The easiest way to configure this is simple

require 'rspec/rabl/rails'

For more detailed configuration just look at that file to see what configurations it is making.

Additionally, there are two config attributes which can be specified using rabl_config:

scope is the rendering context which is passed to create the underlying Rabl::Renderer instance for an example group. If your rabl template is using, for example, view helpers in Rails but your spec is raising a NoMethodError, then you probably want to pass in a scope which includes that helper.

describe "Users which use a Helper to transform data for presentation" do
  class ScopeWithUsersHelper
    include Singleton
    include UsersHelper # could have a method which formats an email address for use below
  end

  rabl_config(:scope => ScopeWithUsersHelper.instance)
  rabl_template { "users/show.rabl" }
  rabl_data(:root => 'user') { user }

  specify { expect(subject).to render_attribute(:formatted_email).with_value('"Kung Fury" <thechosenone@example.com>') }
end

view_paths instructs the Rabl::Renderer where to look for its rabl templates for the purpose of an example group.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request