Skip to content

Instantly share code, notes, and snippets.

@awesome
Forked from joshknowles/gist:19130
Created April 16, 2014 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/10936593 to your computer and use it in GitHub Desktop.
Save awesome/10936593 to your computer and use it in GitHub Desktop.
# 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