Skip to content

Instantly share code, notes, and snippets.

@nbomberger
Last active August 29, 2015 13:56
Show Gist options
  • Save nbomberger/9138914 to your computer and use it in GitHub Desktop.
Save nbomberger/9138914 to your computer and use it in GitHub Desktop.
Simple example of shared_examples in Rspec. Put the _shared in your support folder. Example creates a context where you have access to a logged in user.
# encoding: UTF-8
require 'spec_helper'
describe 'Category JSON Spec', focus: :true do
include_examples 'a logged in user'
describe 'Someaction#index' do
before(:each) { FactoryGirl.create_list(:category, 5) }
subject(:response) do
# you have access to the @user var
end
it 'returns a list of categories' do
pending('Currently in testing branch')
# json[:categories].to_json.should have_json_size(5)
end
end
describe 'Categories#show' do
let(:category) { FactoryGirl.create(:category) }
subject(:response) do
get "/some/get/request",
format: :json
end
let(:valid_json) do
{
category: {
id: category.id,
name: category.name,
icon_class: category.icon_class,
providers: category.providers
}
}.to_json
end
it 'returns the category as] json' do
binding.pry
# pending('Currently in testing branch')
# response.body.should have_json_path('category')
# response.body.should be_json_eql(valid_json)
end
end
end
# encoding: UTF-8
require 'spec_helper'
shared_examples "a logged in user" do
before(:all) do
@user = FactoryGirl.create(:user_api)
end
before(:each) do
login_as @user, scope: :user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment