Skip to content

Instantly share code, notes, and snippets.

@tily
Created October 12, 2010 13:03
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 tily/622133 to your computer and use it in GitHub Desktop.
Save tily/622133 to your computer and use it in GitHub Desktop.
class HerClass
def run; end
end
class MyClass
def initialize; @her = HerClass.new end
def exec; @her.run end
end
describe MyClass, "execメソッドが実行されたとき:" do
# from http://d.hatena.ne.jp/kmrshntr/20100726/1280149431
it "動く (けど、読みづらい気がする)" do
@her_mock = mock("her")
HerClass.should_receive(:new).and_return(@her_mock)
@my = MyClass.new
@her_mock.should_receive(:run)
@my.exec
end
it "動かない (should_receive が Mock オブジェクトを返さないため)" do
HerClass.should_receive(:new).and_return(
mock("her").should_receive(:run)
)
@my = MyClass.new
@my.exec
end
it "動く (Object#tap をつかって Mock オブジェクトをつかまえるやり方)" do
HerClass.should_receive(:new).and_return(
mock("her").tap {|m| m.should_receive(:run) }
)
@my = MyClass.new
@my.exec
end
it "動かない (mock() がブロック引数をとればこう書けるんだけどな…)" do
HerClass.should_receive(:new).and_return(
mock("her") {|m| m.should_receive(:run) }
)
@my = MyClass.new
@my.exec
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment