我試圖讓我的代碼稍微小一點,構建一個方法,看起來像來自RSpec的should_receive ,這裏的情況是,我測試的狀態機,我有幾種方法有這樣的代碼:如何建立鏈式方法,如 - should_receive(:something).with(:params,values).and_return(:something_else)
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')
這些方法是如何構建的?