Skip to content

Instantly share code, notes, and snippets.

@cheald
Created November 23, 2012 08:36
Show Gist options
  • Save cheald/4134547 to your computer and use it in GitHub Desktop.
Save cheald/4134547 to your computer and use it in GitHub Desktop.
% rspec spec.rb --format doc
Foo
when logged in as a user
should == "TEST VALUE"
Finished in 0.00047 seconds
1 example, 0 failures
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
specify {
@active_user.options[:value].should == "TEST VALUE"
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment