Skip to content

Instantly share code, notes, and snippets.

View rhburrows's full-sized avatar

Ryan Burrows rhburrows

View GitHub Profile
module FollowEventTransitionMatcher
class FollowEventTransition
def initialize(event, options)
@event = event
@from = options[:from].is_a?(Array) ? options[:from] : [options[:from]]
@to = options[:to]
end
it "event something should transition from state first to state second" do
Model.should follow_event_transition(:something, :from => :first, :to => :second)
end
rails movie-planning
cd movie-planning
script/generate rspec
script/generate rspec_model Movie
script/generate rspec_model Actor movie_id:integer alive:boolean
rake db:migrate
rake spec
# app/models/actor.rb
class Actor < ActiveRecord::Base
belongs_to :movie
end
# spec/models/actor_spec.rb
require 'spec_helper'
describe Actor do
describe "#kill" do
it "make the actor no longer alive" do
a = Actor.new(:alive => true)
a.kill
a.should_not be_alive
end
# app/models/actor.rb
class Actor < ActiveRecord::Base
def kill
self.alive = false
end
end
# spec/models/actor_spec.rb
describe Actor do
describe "#kill" do
before(:each) do
@movie = mock_model(Movie).as_null_object
@actor = Actor.new(:alive => true, :movie => @movie)
end
it "makes the actor no longer alive" do
@actor.kill
# lib/spec/mocks/methods.rb
module Spec
module Mocks
module Methods
def as_null_object
__mock_proxy.as_null_object
end
end
end
end
# lib/spec/mocks/proxy.rb
module Spec
module Mocks
class Proxy
@options[:null_object] = true
end
end
end
# lib/spec/mocks/mock.rb
module Spec
module Mocks
class Mock
def method_missing(sym, *args, &block)
__mock_proxy.record_message_received(sym, args, block)
begin
return self if __mock_proxy.null_object?
super(sym, *args, &block)
rescue NameError