Create a gist now

Instantly share code, notes, and snippets.

# Example of how to test a module included into multiple models
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Featurable do
class FeaturableObject
include Featurable
attr_accessor :sport, :featured_at
def featured_at?
!@featured_at.nil?
end
end
before :each do
@featurable = FeaturableObject.new
end
describe "can_be_featured_by?" do
context "user is an admin" do
before :each do
@user = stub_model(User, :admin? => true)
end
it "should return true if assigned to a sport" do
@featurable.sport = stub_model(Sport)
@featurable.can_be_featured_by?(@user).should be_true
end
it "should return false if not assigned to a sport" do
@featurable.sport = nil
@featurable.can_be_featured_by?(@user).should be_false
end
end
context "user is not an admin" do
it "should return false" do
user = stub_model(User, :admin? => false)
@featurable.can_be_featured_by?(user).should be_false
end
end
end
describe "featured?" do
specify do
@featurable.featured_at = Time.now
@featurable.should be_featured
end
specify do
@featurable.featured_at = nil
@featurable.should_not be_featured
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment