StateMachine
을 Factory Girl
으로 테스트하는 데 문제가 있습니다. 그것은 공장 소녀가 객체를 초기화하는 방식으로 보입니다.FactoryGirl 및 StateMachine을 사용하여 동적 초기 상태 테스트
내가 누락되었거나 쉽지 않은가?
class Car < ActiveRecord::Base
attr_accessor :stolen # This would be an ActiveRecord attribute
state_machine :initial => lambda { |object| object.stolen ? :moving : :parked } do
state :parked, :moving
end
end
Factory.define :car do |f|
end
그래서, 초기 상태는 stolen
속성을 초기화하는 동안 설정되어 있는지 여부에 따라 달라집니다.
Car.new(:stolen => true)
## Broadly equivalent to
car = Car.new do |c|
c.attributes = {:stolen => true}
end
car.initialize_state # StateMachine calls this at the end of the main initializer
assert_equal car.state, 'moving'
그러나,이 흐름이 더 같은 의미 :이 액티브가 초기화의 일부로 속성을 설정하기 때문에, 잘 작동하는 것 같다
Factory(:car, :stolen => true)
## Broadly equivalent to
car = Car.new
car.initialize_state # StateMachine calls this at the end of the main initializer
car.stolen = true
assert_equal car.state, 'moving' # Fails, because the car wasn't 'stolen' when the state was initialized