1
我有以下的對象和關係,防止狀態耦合?
Lecture >- Tests
Test >- Questions
業務規則
When the lecture is started, a test can be given
If a test is being given, questions can be asked
推理
Therefore questions shouldn't be asked if the lecture hasn't been started.
問題型號
class Question
belongs_to :test
belongs_to :lecture, :through => :test
def ask_question
raise "Test not started!" unless test.started?
raise "Lecture not started!" unless lecture.started?
end
end
顯然,問題模型的狀態現在與測試和課堂狀態相結合。
當創建單元測試時,爲了測試這個,我需要設置所有這個狀態,這很難實現,特別是當業務案例變得越來越複雜時。
我該如何避免這種情況?