Created
June 26, 2013 01:54
-
-
Save joeletizia/5864163 to your computer and use it in GitHub Desktop.
Example of mocking and stubing with dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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