ruby-on-rails - 使用 rspec 测试 aasm 状态转换

标签 ruby-on-rails ruby rspec state-machine aasm

我正在使用 aasm gem 处理项目中的状态转换。我有一个看起来像这样的简单模型:

class TransferPostingBid < ActiveRecord::Base
  include AASM

  aasm :status do
    state :offered, initial: true
    state :wait_for_pap_confirmation
    state :confirmed_by_pap
    state :retracted_by_pap

    event :pap_choosed do
      transitions from: :offered, to: :wait_for_pap_confirmation
    end

    event :confirmed_by_pap do
      transitions from: :wait_for_pap_confirmation, to: :confirmed_by_pap
    end

    event :retracted_by_pap do
      transitions from: :wait_for_pap_confirmation, to: :retracted_by_pap
    end
  end
end

我正在尝试使用内置在 rspec 匹配器中的 aasm 测试转换:

require 'rails_helper'

describe TransferPostingBid, type: :model do
  describe 'state transitions' do
    let(:transfer_posting_bid) { TransferPostingBid.new }

    it 'has default state' do
      expect(transfer_posting_bid).to transition_from(:offered).to(:wait_for_pap_confirmation).on_event(:pap_choosed)
    end
  end
end

当我运行此规范时,它会返回以下错误:

 AASM::UnknownStateMachineError:
   There is no state machine with the name 'default' defined in TransferPostingBid!

我该如何解决这个问题?

最佳答案

您可以尝试使用 #on 方法来指定您正在测试的状态机:

transition_from(:offered).to(:wait_for_pap_confirmation).on_event(:pap_choosed).on(:status)

关于ruby-on-rails - 使用 rspec 测试 aasm 状态转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39273745/

相关文章:

ruby-on-rails - rails 测试 : test image_tag on view

css - SASS 上的引用图像

ruby-on-rails - rails 4 has_many :through not saving associations

ruby-on-rails - 返回无效模型进行测试

ruby-on-rails - 未初始化常量 MiniTest::测试错误

mysql - 海量记录更新——性能优化

css - Ruby on Rails + Rspec : Syntax for css selector containing instance variable

ruby-on-rails - 如何在rails中获取当前路线

ruby-on-rails - 如何为我的测验应用程序布局表单以及发布到哪里?

ruby - Ruby 中的斐波那契数列(递归)