我有以下对象和关系,
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
很明显,问题模型的状态现在与测试和课堂的状态相耦合。
在创建单元测试时,为了测试它,我需要设置所有这些状态,这变得非常笨拙,尤其是当业务案例变得越来越复杂时。
我怎样才能避免这种情况?