Skip to content
This repository
tree: f6a4d09877
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 101 lines (80 sloc) 2.925 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class Post
  attr_accessor :published, :public, :deleted
 
  def attributes=(attributes={}, guard=true)
    attributes.each do |key, value|
      send(:"#{key}=", value) unless guard
    end
  end

  def self.human_name(*args)
    "MyPost"
  end
end

describe Post do
  it "should use human name on description" do
    self.class.description.should == "MyPost"
  end

  describe "default attributes as a hash" do
    subject_attributes :deleted => true

    it "should set the subject with deleted equals to true" do
      subject.deleted.should be_true
    end

    it "should not change the description" do
      self.class.description.should == "MyPost default attributes as a hash"
    end
  end

  describe "default attributes as a proc" do
    subject_attributes { my_attributes }

    it "should set the subject with deleted equals to true" do
      subject.deleted.should be_true
    end

    it "should not change the description" do
      self.class.description.should == "MyPost default attributes as a proc"
    end

    def my_attributes
      { :deleted => true }
    end
  end

  describe :published => true do
    it "should set the subject with published equals to true" do
      subject.published.should be_true
    end

    it "should generate a readable description" do
      self.class.description.should == "MyPost when published is true"
    end

    it "should call human name attribute on the described class" do
      Post.should_receive(:human_attribute_name).with("comments_count", :locale => :en).and_return("__COMMENTS__COUNT__")
      self.class.describe(:comments_count => 5) do
        self.description.should == 'MyPost when published is true and __comments__count__ is 5'
      end
    end

    describe :public => false do
      it "should nest subject attributes" do
        subject.published.should be_true
        subject.public.should be_false
      end

      it "should nest descriptions" do
        self.class.description.should == "MyPost when published is true and public is false"
      end

      describe "default attributes as a hash" do
        subject_attributes :deleted => true

        it "should merge describe attributes with subject attributes" do
          subject.published.should be_true
          subject.public.should be_false
          subject.deleted.should be_true
        end
      end
    end
  end

  describe :published => true, :public => false do
    it "should set the subject with published equals to true and public equals to false" do
      subject.published.should be_true
      subject.public.should be_false
    end

    it "should include both published and public in descriptions" do
      self.class.description.should match(/MyPost/)
      self.class.description.should match(/public is false/)
      self.class.description.should match(/published is true/)
    end
  end
end
Something went wrong with that request. Please try again.