Skip to content

Instantly share code, notes, and snippets.

@joeletizia
Created June 26, 2013 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeletizia/5864163 to your computer and use it in GitHub Desktop.
Save joeletizia/5864163 to your computer and use it in GitHub Desktop.
Example of mocking and stubing with dependency injection
module Example
class Dog
attr_reader :stomach
def initialize(stomach)
@stomach = stomach
end
def hungry?
stomach.empty?
end
end
end
module Example
describe Dog do
subject { Dog.new(stomach)}
describe "#new" do
let(:stomach) { double(:stomach) }
it "should take a stomach" do
subject.stomach.should == stomach
end
end
describe "#hungry?" do
context "when stomach is empty" do
let(:stomach) { double(:stomach) }
before do
stomach.stub(:empty?) { true }
end
it "is hungry" do
subject.hungry?.should be_true
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment