单元测试POCO类中的属性属性

时间:2011-08-03 15:01:22

标签: unit-testing nhibernate poco model-associations

我正在开发一个NHibernate持久化网站,因此使用POCO类型类。我想采用单元测试驱动的方法来改进我的实践,并直接进入我认为是一个大问题。

我有两个相关的课程...... EvaluationSession(可能代表一个学年)和EvaluationWindow(代表学年的条款)。这里有一个NHibernate映射的一对多关系,NHibernate映射很容易实现,有so much documentation on it

这是一个简化的图表:

class diagram

EvaluationWindow看起来像这样:

Public Class EvaluationWindow

   Private _dateFinish As DateTime
   Private _dateStart As DateTime
   Private _evaluationSession As EvaluationSession

   <EvaluationWindowFinishDate()> _
   Public Overridable Property DateFinish() As DateTime
       Get
           Return _dateFinish
       End Get
       Set(ByVal value As DateTime)
           _dateFinish = value
    End Set
   End Property

   <EvaluationWindowStartDate()> _
   Public Overridable Property DateStart() As DateTime
       Get
           Return _dateStart
       End Get
       Set(ByVal value As DateTime)
           _dateStart = value
       End Set
   End Property

   Public Overridable ReadOnly Property EvaluationSession() As EvaluationSession
       Get
           Return _evaluationSession
       End Get
   End Property

End Class

我为EvaluationWindow DateStart和DateFinish创建了custom validation attribute。 EvaluationWindow.DateStart和EvaluationWindow.DateFinish 不得超出包含EvaluationSession的范围。

所以验证器做了这样的事情:

If EvaluationWindow.DateStart >= EvaluationWindow.EvaluationSession.DateStart And
   EvaluationWindow.DateStart <= EvaluationWindow.EvaluationSession.DateFinish Then

问题是在没有发生数据访问时使用域对象时,EvaluationWindow.EvaluationSession属性始终为null。 EvaluationWindow.EvaluationSession永远不会被实例化以使此测试成功:

<TestMethod()> Public Sub ValidateEvaluationWindowStartDate()

    '   Test evaluation window start date validation
    Dim TheValidator As New Validator
    Dim EvalWindow As EvaluationWindow
    Dim EvalSession As EvaluationSession

    '   Check a valid date assignment is validated correctly .
    EvalSession = New EvaluationSession
    EvalSession.DateStart = CDate("01/01/2011")
    EvalSession.DateFinish = CDate("31/12/2011")
    EvalWindow = New EvaluationWindow()
    EvalWindow.DateStart = CDate("02/01/2011")
    EvalWindow.DateFinish = CDate("30/12/2011")
    EvalSession.EvaluationWindows.Add(EvalWindow)
    Assert.IsTrue(TheValidator.IsValid(EvalSession.EvaluationWindows(0)), 
                 "Evaluation window start date was within the date range of it's " & _
                 "containing evaluation session, yet failed validation.")

End Sub

据我所知,我不能在EvaluationWindow中添加一个构造函数,它将实例化的EvaluationSession作为参数,因为这会导致NHib出现问题,但我想在域层测试这些验证属性,因为我已阅读并同意将业务规则放入域中是有意义的。

我对NHib / OOP比较陌生,这个让我头疼,但我很想确定一些最好的做法。关于这个问题的任何好的指示都将受到重视:)

0 个答案:

没有答案