Skip to content

Instantly share code, notes, and snippets.

@cheald
Created November 23, 2012 08:52
Show Gist options
  • Select an option

  • Save cheald/4134613 to your computer and use it in GitHub Desktop.

Select an option

Save cheald/4134613 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext/array'
class FactoryGirl
attr_accessor :options
def self.create(model, options = {})
new(model, options)
end
def initialize(model, options)
@model = model
@options = options
end
end
class DatabaseCleaner
def self.clean; end
end
def authenticated_as(roles, options = nil, &block)
Array.wrap(roles).each do |role|
context "when logged in as a #{role}" do
before(:all) {
options = self.instance_eval &options if options.is_a? Proc
@user = FactoryGirl.create role, *Array.wrap(options)
}
after(:all) { DatabaseCleaner.clean }
before(:each) { @active_user = @user }
after(:each) { @active_user = nil }
instance_eval &block
end
end
end
describe "Foo" do
let(:test_val) { "Test Value" }
authenticated_as :user, Proc.new{ {value: test_val.upcase} } do
context "A case to populate params" do
let(:params) { "original recipe" }
specify { params.should == "original recipe" }
end
context "and a case to reuse them" do
let(:params) { "extra crispy" }
specify { params.should == "extra crispy"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment