在使用 Factory Girl 工厂进行测试时,如何覆盖 AASM 中的 :set_initial_state?

发布于 2024-07-25 20:13:46 字数 1290 浏览 9 评论 0 原文

更新

答案如下。 如果链接的站点消失,您可以使用 mocha 来存根初始状态并防止覆盖,如...

require 'mocha'
class OrderTest < ActiveSupport::TestCase
  def setup
    Order.any_instance.stubs(:set_initial_state)
    @order = Factory(:order, :state => "other_state")
  end

  ...
end

原始问题

我当前正在运行 Acts As State Machine Rails 插件(顺便说一句,它节省了大量时间)并有一些挑战与 Factory Girl 一起使用它(也很棒)。

我希望能够在使用工厂创建对象时设置对象状态。 提出这个问题的一般方法是“在使用工厂创建对象时如何调用类方法?”

class Transporter < ActiveRecord::Base
  validates_presence_of :company_name, :on => :update
  acts_as_state_machine :initial => :created, :column => 'status'
  state :created
  state :active
  state :inactive, :after => :inactivate_transporter_activity
end

Factory.define :transporter do |f|
  f.sequence(:company_name) {|n| "transporter_company#{n}"}
end

>> t=Factory(:transporter)
=> <Transporter ... status: "created">
>> t=Factory(:transporter, :status => 'active')
=> <Transporter ... status: "created"> #as expected, changes state back
>> t.activate!
=> true
>> t
=> <Transporter ... status: "active">

我随时可以调用 t.activate! 每个测试中的方法,但这将使我的测试变得脆弱。 我正在寻找一种在工厂创建级别运行此方法或在factory.rb 中设置它的方法。

谢谢...

Update

Answered below. In case the linked site disappears, you can use mocha to stub the initial state and prevent overwriting as in ...

require 'mocha'
class OrderTest < ActiveSupport::TestCase
  def setup
    Order.any_instance.stubs(:set_initial_state)
    @order = Factory(:order, :state => "other_state")
  end

  ...
end

Original Question

I am currently running the Acts As State Machine Rails Plugin (has been a huge time saver, incidentally) and having some challenges using it with Factory Girl (also wonderful).

I want to be able to set the object state when I create the object with Factories. A generalized way of asking this question is "how do you call class methods when creating a object with Factories?"

class Transporter < ActiveRecord::Base
  validates_presence_of :company_name, :on => :update
  acts_as_state_machine :initial => :created, :column => 'status'
  state :created
  state :active
  state :inactive, :after => :inactivate_transporter_activity
end

Factory.define :transporter do |f|
  f.sequence(:company_name) {|n| "transporter_company#{n}"}
end

>> t=Factory(:transporter)
=> <Transporter ... status: "created">
>> t=Factory(:transporter, :status => 'active')
=> <Transporter ... status: "created"> #as expected, changes state back
>> t.activate!
=> true
>> t
=> <Transporter ... status: "active">

I can always call the t.activate! method within every test, but this will make my tests brittle. I'm looking for a way to run this method at Factory creation level or set it within factory.rb.

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2024-08-01 20:13:47

您可以使用模拟框架(mocha)来覆盖 set_initial_state 并获取对象的正确状态。

>> require 'mocha'
=> []
>> Transporter.any_instance.stubs(:set_initial_state)
=> #<Mocha::Expectation:0x21ee6e4 ...
>> t = Factory(:transporter, :state => "active")
=> #<Transporter ... state: "active">

这里

You can use a mocking framework (mocha) to override set_initial_state and get the correct state on your object.

>> require 'mocha'
=> []
>> Transporter.any_instance.stubs(:set_initial_state)
=> #<Mocha::Expectation:0x21ee6e4 ...
>> t = Factory(:transporter, :state => "active")
=> #<Transporter ... state: "active">

Idea stolen from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文