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
 

你可能感兴趣的:(Ruby,rspec)