Destroy All Software Screencast Reviews

Fast Tests With and Without Rails Episode Review

The specs in the final solution abuses mock by mocking the ActiveRecord API. The expectation mocks update_attributes. He says the solution only depends on RSpec and specs run outside of Rails, so the specs run fast. The problem is that the dependency on Rails exists because of the coupling created by the mocking of ActiveRecord. The tests are now brittle and can break when you upgrade Rails if the active record API changes.

Before

alice.should_receive(:update_attributes).with(happiness => 60)

This ties the test to implementation. Here is a better way:

After

expect do
  alice.nice_walk
end.to increase(:happiness).by(60)

This simplifies outcomes and does not break when implementation of ActiveRecord changes (for instance, when you upgrade the Rails framework).

Growing a Test Suite Episode Review

This is the review of the solution discussed in Growing a Test Suite screencast. Here is a list of things that the solution violates:

  1. Separate Command from Query. digest() method is a command. digest() method should not return any value
  2. Allocate responsibilities to the right class. digest() method does not belong to the Cheese class. Semantically it does not make any sense. Only a living organism can digest food.
  3. Law of Demeter.

Here is my solution:

class Walrus
  attr_reader :energy

  def initialize
    @energy = 0
  end

  def eat(food)
    digest
    @energy += food.energy
  end

  private

  def digest
    p 'Digesting happens inside my body...'
  end
end

class Cheese
  def energy
    100
  end
end


cheese = Cheese.new
w  = Walrus.new

p "Energy before eating is : #{w.energy}"

w.eat(cheese)

p "Energy now is : #{w.energy}" 


Related Articles


Create your own user feedback survey