spec/statesman/adapters/shared_examples.rb in statesman-0.3.0 vs spec/statesman/adapters/shared_examples.rb in statesman-0.4.0

- old
+ new

@@ -10,24 +10,28 @@ # instance and saves metadata to it. # history: Returns the full transition history # last: Returns the latest transition history item # shared_examples_for "an adapter" do |adapter_class, transition_class| - let(:adapter) { adapter_class.new(transition_class, model) } - let(:before_cbs) { [] } - let(:after_cbs) { [] } + let(:observer) do + result = double(Statesman::Machine) + result.stub(:execute) + result + end + let(:adapter) { adapter_class.new(transition_class, model, observer) } describe "#initialize" do subject { adapter } its(:transition_class) { should be(transition_class) } its(:parent_model) { should be(model) } its(:history) { should eq([]) } end describe "#create" do + let(:from) { :x } let(:to) { :y } - let(:create) { adapter.create(to, before_cbs, after_cbs) } + let(:create) { adapter.create(from, to) } subject { -> { create } } it { should change(adapter.history, :count).by(1) } context "the new transition" do @@ -41,71 +45,62 @@ context "with no previous transition" do its(:sort_key) { should be(0) } end context "with a previous transition" do - before { adapter.create(:x, before_cbs, after_cbs) } + before { adapter.create(from, to) } its(:sort_key) { should be(10) } end end context "with before callbacks" do - let(:callback) { double(call: true) } - let(:before_cbs) { [callback] } - it "is called before the state transition" do - callback.should_receive(:call).with do |passed_model, transition| - expect(passed_model).to be(model) - expect(transition).to be_a(adapter.transition_class) - expect(adapter.history.length).to eq(0) + observer.should_receive(:execute).with do + |phase, from_state, to_state, transition| + expect(adapter.history.length).to eq(0) if phase == :before end.once - - adapter.create(:x, before_cbs, after_cbs) + adapter.create(from, to) expect(adapter.history.length).to eq(1) end end context "with after callbacks" do - let(:callback) { double(call: true) } - let(:after_cbs) { [callback] } - it "is called after the state transition" do - callback.should_receive(:call).with do |passed_model, transition| - expect(passed_model).to be(model) - expect(adapter.last).to eq(transition) + observer.should_receive(:execute).with do + |phase, from_state, to_state, transition| + expect(adapter.last).to eq(transition) if phase == :after end.once - adapter.create(:x, before_cbs, after_cbs) + adapter.create(from, to) end end context "with metadata" do let(:metadata) { { "some" => "hash" } } - subject { adapter.create(to, before_cbs, after_cbs, metadata) } + subject { adapter.create(from, to, metadata) } its(:metadata) { should eq(metadata) } end end describe "#history" do subject { adapter.history } it { should eq([]) } context "with transitions" do - let!(:transition) { adapter.create(:y, before_cbs, after_cbs) } + let!(:transition) { adapter.create(:x, :y) } it { should eq([transition]) } context "sorting" do - let!(:transition) { adapter.create(:y, before_cbs, after_cbs) } - let!(:transition2) { adapter.create(:y, before_cbs, after_cbs) } + let!(:transition2) { adapter.create(:x, :y) } subject { adapter.history } it { should eq(adapter.history.sort_by(&:sort_key)) } end end end describe "#last" do before do - adapter.create(:y, before_cbs, after_cbs) - adapter.create(:z, before_cbs, after_cbs) + adapter.create(:x, :y) + adapter.create(:y, :z) end subject { adapter.last } it { should be_a(transition_class) } specify { expect(adapter.last.to_state.to_sym).to eq(:z) }