Question

I'm working on a project that uses the state_machine gem which I haven't used before.

In one of the controllers there is:

if @booking.propose_offer_as_guest
  # do some stuff
end

In my test that I'm writing for the controller, @booking.propose_offer_as_guest is always returning false and I don't know why.

The state machine definition in app/models/booking.rb looks like this:

 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

I don't see anything that seems like it could make propose_offer_as_guest not work. What's the deal?

Edit: here is my test:

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
Was it helpful?

Solution

Turns out the reason is because my @booking was invalid. I suspect this question will end up getting closed due to its narrowness.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top