Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created August 30, 2012 15:17
Show Gist options
  • Save jsanders/3530602 to your computer and use it in GitHub Desktop.
Save jsanders/3530602 to your computer and use it in GitHub Desktop.
Test case for gimme class method spies
require 'spec_helper'
module Gimme
class ChairFactory
def self.build
raise RuntimeError.new "unimplemented feature"
end
def self.destroy
raise RuntimeError.new "unimplemented feature"
end
end
module StoolBuilder
def build
raise RuntimeError.new "unimplemented feature"
end
end
class StoolFactory
extend StoolBuilder
def self.destroy
raise RuntimeError.new "unimplemented feature"
end
end
describe SpiesOnClassMethod do
shared_examples_for "it spies on class methods" do
describe "normal class method spy" do
Given(:invocation) { lambda { ChairFactory.build } }
Then { invocation.should_not raise_error }
context "upon reset" do
When { Gimme.reset }
Then { invocation.should raise_error }
end
end
describe "imaginary class method spy" do
Given(:invocation) { lambda { ChairFactory.fantasy } }
Then { invocation.should_not raise_error }
context "upon reset" do
When { Gimme.reset }
Then { invocation.should raise_error }
end
end
end
context "classical API" do
it_behaves_like "it spies on class methods" do
subject { SpiesOnClassMethod.new(ChairFactory) }
Given { subject.spy(:build) }
Given { subject.raises_no_method_error = false }
Given { subject.spy(:fantasy) }
end
end
context "gimme DSL" do
it_behaves_like "it spies on class methods" do
Given { spy_on(ChairFactory, :build) }
Given { spy_on!(ChairFactory, :fantasy) }
end
end
context "with methods included from a module" do
context "classical API" do
it_behaves_like "it spies on class methods" do
subject { SpiesOnClassMethod.new(StoolFactory) }
Given { subject.spy(:build) }
Given { subject.raises_no_method_error = false }
Given { subject.spy(:fantasy) }
end
end
context "gimme DSL" do
it_behaves_like "it spies on class methods" do
Given { spy_on(StoolFactory, :build) }
Given { spy_on!(StoolFactory, :fantasy) }
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment