Created
November 19, 2012 13:40
-
-
Save hoshinotsuyoshi/4110703 to your computer and use it in GitHub Desktop.
RSpecのモックとスタブの自分で作った
This file contains 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
# coding: utf-8 | |
#まずクラス定義 | |
class MyClass | |
def hook | |
square(3) | |
end | |
def square(n) | |
n * n | |
end | |
def what_time_is_it | |
Time.now | |
end | |
end | |
#こっからテスト | |
require 'rspec' | |
describe 'MyClassのインスタンス' do | |
before(:all) do | |
@a = MyClass.new | |
end | |
it 'は、クラスはMyClassとなる' do | |
@a.class.should == MyClass | |
end | |
#mock | |
it 'は、メソッドhookを呼ぶときメソッドsquareを呼ぶ' do | |
@a.should_receive(:square).with(3).and_return(9) | |
@a.hook | |
end | |
#stub | |
it 'メソッドwhat_time_is_itは現在の時刻を返す' do | |
Time.stub!(:now).and_return("午後1時") | |
@a.what_time_is_it.should == "午後1時" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment