Giter Club home page Giter Club logo

rspec_rabl's Introduction

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" <[email protected]>') }
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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.