不知道为什么state_machine转换返回false

时间:2014-04-23 14:55:31

标签: ruby-on-rails state-machine

我正在开发一个使用我以前没用过的state_machine宝石的项目。

在其中一个控制器中有:

if @booking.propose_offer_as_guest
  # do some stuff
end

在我的测试中,我正在为控制器写作,@booking.propose_offer_as_guest总是返回假,我不知道为什么。

app/models/booking.rb中的状态机定义如下所示:

 state_machine :initial => :unaccepted_by_guest_or_host do                                                                                                                 [353/467]

    #states: 
    # guest_accepted_offer
    # guest_proposed_offer
    # host_proposed_offer
    # host_accepted_offer
    # paid (when paid for)
    # canceled

    # to be used only by the host
    event :propose_offer_as_host do
      transition :unaccepted_by_guest_or_host => :host_proposed_offer
    end

    # to be used only by the host
    event :revise_offer_as_host do
      transition [:guest_proposed_offer, :host_proposed_offer, :host_accepted_offer, :guest_accepted_offer] => :host_proposed_offer
    end

    # to be used only by the host
    event :accept_offer_as_host do
      transition :guest_proposed_offer => :host_accepted_offer
    end

    # to be used only by the guest
    event :propose_offer_as_guest do
      transition :unaccepted_by_guest_or_host => :guest_proposed_offer
    end

    # to be used only by the guest
    event :revise_offer_as_guest do
      transition [:host_proposed_offer, :guest_proposed_offer, :host_accepted_offer, :guest_accepted_offer] => :guest_proposed_offer
    end

    # to be used only by the guest
    event :accept_offer_as_guest do
      transition :host_proposed_offer => :guest_accepted_offer
    end

    event :mark_as_paid do
      transition [:guest_accepted_offer, :host_accepted_offer] => :paid
    end

    # process the credit card safely, if this fails with an exception, state won't be transitioned
    # around_transition on: :pay do |booking, transition, block|
    #   booking.transaction do
    #     block.call # block is an event's proc. we need to perform it
    #     booking.process_credit_card
    #   end
    # end

    event :cancel do
      transition all => :canceled
    end
  end

我没有看到任何看起来像是可以使proposal_offer_as_guest不起作用的东西。这笔交易是什么?

修改:这是我的测试:

require 'spec_helper'

describe BookingsController do

  include Capybara::DSL
  include Capybara::RSpecMatchers
  render_views

  describe "POST create" do
    before do
      @params = {
        "booking" => {
          "start_date" => "April 24, 2014",
          "end_date"   => "April 25, 2014",
          "guests"     => "1",
          "message"    => "test",
          "email"      => "john.smith@example.com",
          "captcha"    => "wet"
        }
      }
    end

    it "sends new booking notification email" do
      expect(BookingMailer).to receive(:new_booking_notification)
      post :create, @params
    end
  end
end

1 个答案:

答案 0 :(得分:0)

原因是我的@booking无效。我怀疑这个问题最终会因为它的狭窄而被关闭。