我试图让我的代码更小一些,构建一个看起来像 RSpec 中的 should_receive 的方法,这里的情况是我正在测试一个状态机,我有几个方法,代码如下:
context "State is unknown" do
before do
@obj = create_obj(:state => 'unknown')
end
context "Event add" do
it 'should transition to adding if not in DB' do
@obj.add
@obj.state.should == 'adding'
end
it 'should transition to linking if already in DB' do
create_obj_in_db
@obj.add
@obj.state.should == 'linking'
end
end
end
我想将这些代码行替换为类似于以下内容:
@obj.should_receive(:add).and_transition_to('adding')
@obj.should_receive(:modify).and_transition_to('modifying')
这些方法是如何构建的?