天天看點

ruby rspec mock study

describe "Mocker" do

  it "should be able to call mock()" do
    mock = mock("poke me")#建立一個mock對象
    mock.should_receive(:poke).once.with(1,2,3)#測試這個對象是否會接收一次poke方法
    mock.poke(1,2,3)
    #test the arg type
    mock.should_receive(:msg).with(an_instance_of(Fixnum))
    mock.msg(2)
  end
#測試mock對象接收方法的順序
  it "should fail when messages are received out of order" do
    mock = mock("one two three")
    mock.should_receive(:one).ordered
    mock.should_receive(:two).ordered
    mock.should_receive(:three).ordered
    mock.one
    mock.two
    mock.three
  end
end

           

 之後運作

rspc file_name