code school testing with spec(2)-1

####Predicate Matchers zombie.rb

class Zombie < ActiveRecord::Base
  validates :name, presence: true

  def genius?
    iq >= 3
  end
end

old:

describe Zombie do
  it 'includes a tweet' do
    tweet = Tweet.new
    zombie = Zombie.new(tweets: [tweet])
    zombie.tweets.first.should == tweet
  end
end

new:

 describe Zombie do 
   it 'includes a tweet' do 
   tweet = Tweet.new 
   zombie = zombie.new(tweets: [tweet])
   zombie.tweets.should include(tweet)
   end
 end

###Change Matcher zombie.rb

class Zombie < ActiveRecord::Base
  validates :name, presence: true
  validates :iq, numericality: true

  def eat_brains
    self.iq += 3
  end
end

old:

describe Zombie do
  it 'gains 3 IQ points by eating brains' do
    zombie = Zombie.new
    zombie.iq.should == 0
    zombie.eat_brains
    zombie.iq.should == 3
  end
end

method:

describe Zombie do
  it 'gains 3 IQ points by eating brains' do
    zombie = Zombie.new
    expect { <some method> }.to change { <some value> }.by(<some amount>)
  end
end

new:

describe Zombie do 
  it 'gains 3 IQ points by eating brains' do 
  zombie = Zombie.new 
  expect { zombie.eat_brains }.to change { zombie.iq }.by(3)
  end
end

###HAVE MATCHER tweet.rb

class Tweet < ActiveRecord::Base
  attr_accessible :message
  belongs_to :zombie
  validates :message, presence: true
end

zombie.rb

class Zombie < ActiveRecord::Base
  attr_accessible :name
  validates :name, presence: true
  has_many :tweets
end
view raw

zombie_spec.rb old:

describe Zombie do
  it 'increases the number of tweets' do
    zombie = Zombie.new(name: 'Ash')
    zombie.tweets.new(message: "Arrrgggggggghhhhh")
    zombie.tweets.count.should > 0
  end
end

new:

describe Zombie do 
  it 'increases the number of tweets' do 
    zombie = Zombie.new(name: 'Ash')
    zombie.tweets.new(message: "Arrrgggggggghhhhh")
    zombie.should have(1).tweet
  end
end

###RAISES AN ERROR tweet.rb

class Tweet < ActiveRecord::Base
  attr_accessible :message
  belongs_to :zombie
  validates :message, presence: true
end

zombie.rb

class Zombie < ActiveRecord::Base
  validates :name, presence: true

  class NotSmartEnoughError < StandardError; end

  def genius?
    iq >= 3
  end

  def make_decision!
    raise NotSmartEnoughError unless genius?
    return true
  end
end
view raw

zombie_spec.rb old:

describe Zombie do
  it 'raises a Zombie::NotSmartEnoughError if not able to make a decision' do
    zombie = Zombie.new
    begin
      zombie.make_decision!
    rescue Zombie::NotSmartEnoughError => e
      e.should be_an_instance_of(Zombie::NotSmartEnoughError)
    end
  end
end

method:

describe Zombie do
  it 'raises a Zombie::NotSmartEnoughError if not able to make a decision' do
    zombie = Zombie.new
    expect { <some method> }.to raise_error(<some error>)
  end
end

new:

describe Zombie do 
  it 'raises a Zombie::NotSmartEnoughError if not able to make a decision' do
    zombie = Zombie.new
    expect{ zombie.make_decision! }.to raise_error(Zombie::NotSmartEnoughError)
  end
end