2012-02-16 5 views
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 

質問モデルの状態がテストとクラスの状態に結びついていることは明らかです。

単体テストを作成する際には、これをテストするために、この状態をすべて設定する必要があります。これは、ビジネスケースがますます複雑になるにつれて、かなり扱いにくくなります。

どうすればこの問題を回避できますか?

答えて

2

私はRubyの関連付けには慣れていませんが、何らかの形でデータモデルが実行時ロジックと共存するようです。

質問とテストのデータモデルを作成する場合は、テスト全体で自分の質問を再利用し、準備されたテスト(質問のセット)を講義を通じて再利用したいと考えています。その場合、私は、その構造から

class Lecture 
    has_and_belongs_to_many :tests 
end 

class Test 
    has_and_belongs_to_many :lectures 
    has_and_belongs_to_many :questions 
end 

class Question 
    has_and_belongs_to_many :tests 
end 

別に何かを書きたい、私はリアルタイムの講義、テスト、質問と結果の概念に対応するいくつかの構造を持っていると思います。結果は、与えられた生徒によるリアルタイムの質問に答える試みです。

また、講義セッションの状態のチェックをテストセッションに「委任」したいと思います。何らかの理由でテストセッションを開始できない場合、質問セッションも開始できません。

質問セッションをユニットテストするには、テストセッションを模擬したり、講義セッションを模擬するために必要なテストセッションを単体テストするだけで済みます。

class Lecture_Session 
    has_many :tests_sessions 
    belongs_to :lecture 
end 

class Test_Session 
    belongs_to :lecture_session 
    belongs_to :test 
    has_many :question_sessions 

    def validate 
    raise "Lecture not started!" unless lecture_session.started? 
    end 
end 

class Question_Session 
    belongs_to :question 
    belongs_to :test_session 

    def validate 
    raise "Test not started!" unless test_session.started? 
    end 
end 

class Result 
    belongs_to :lecture_session 
    belongs_to :test_session 
    belongs_to :question_session 
    belongs_to :student 

    def validate 
    raise "Question is not active!" unless question_session.active? 
    end 
end 

これが役立ちます。

関連する問題