【问题标题】:How to unit test an array of dates?如何对日期数组进行单元测试?
【发布时间】:2016-04-12 11:47:10
【问题描述】:

我正在学习如何进行单元测试,在这里完成 newb。我构建了这个函数,它返回一个给定作为字符串传递的初始日期的日期数组。

我看到了它生成的骨架代码。我理解它在做什么,但我无法想出一种方法来对其进行编码,以使预期值是一个包含 any 数量的 whatever 日期的数组。

有什么建议吗?

我写的函数:

Public Function getHolidayList(ByVal startingDate As String) As Date()
    Const DATE_FORMAT As String = "mm/dd/yyyy"
    Dim ERROR_DATE() As Date = {Date.Parse("06/06/6666")}
    Dim commandText As String
    Dim command As OracleCommand
    Dim dataSet As New DataSet
    Dim connection As OracleConnection

    If startingDate <> Nothing AndAlso startingDate <> "" AndAlso Not startingDate.Equals(DBNull.Value) Then
        startingDate = startingDate.Trim
        Try
            Date.Parse(startingDate)
        Catch ex As Exception
            Return ERROR_DATE
        End Try
    Else
        Return ERROR_DATE
    End If

    If ConfigurationManager.AppSettings("SYSTEM") = "TEST" Then
        connection = New OracleConnection(ConnectionStrings("HRTEST").ConnectionString)
    Else
        connection = New OracleConnection(ConnectionStrings("HRIS").ConnectionString)
    End If

    commandText = "select holiday.h_date from holiday_t1 holiday where holiday.h_date between " & _
      "to_date('" & startingDate & "', " & DATE_FORMAT & "') and " & _
      "to_date('" & Date.Now.ToShortDateString & ", " & DATE_FORMAT & "')"

    command = New OracleCommand(ConfigurationManager.AppSettings("HR_SCHEMA"), connection)
    command.CommandType = CommandType.Text
    command.CommandText = commandText

    connection.Open()
    Dim dataAdapter As New OracleDataAdapter(command)

    dataAdapter.Fill(dataSet)
    connection.Close()
    connection.Dispose()

    Dim holidays(dataSet.Tables(0).Rows.Count - 1) As Date
    For i As Integer = 0 To dataSet.Tables(0).Rows.Count - 1
        holidays(i) = dataSet.Tables(0).Rows(i).Item(0)
    Next

    Return holidays
End Function

自动生成的骨架单元测试代码:

<TestMethod(), _
 HostType("ASP.NET"), _
 AspNetDevelopmentServerHost("C:\Users\url\here", "/projectRootDirectory"), _
 UrlToTest("http://localhost/projectRootDirectory")> _
Public Sub getHolidayListTest()
    Dim target As fmlaDB_Accessor = New fmlaDB_Accessor ' TODO: Initialize to an appropriate value
    Dim startingDate As String = String.Empty ' TODO: Initialize to an appropriate value
    Dim expected() As DateTime = Nothing ' TODO: Initialize to an appropriate value
    Dim actual() As DateTime
    actual = target.getHolidayList(startingDate)
    Assert.AreEqual(expected, actual)
    Assert.Inconclusive("Verify the correctness of this test method.")
End Sub

【问题讨论】:

    标签: .net arrays vb.net unit-testing date


    【解决方案1】:

    要使其成为真正的单元测试,您需要将其与数据库隔离。您可以通过将 db 调用移动到您可以模拟的另一个类来将其与数据库隔离。这将使您可以更好地控制返回的内容,因为依赖于数据库中某些值的测试非常脆弱,并且会随着时间的推移而退化,如果测试运行程序无法与数据库对话,则会给出假阴性。

    就目前而言,您可以为其进行集成测试。至于断言什么,您可以依靠编译器为您提供预期值。

    <TestMethod> _
    Public Sub Test()
        Dim [date] = DateTime.Now
        Dim result = getHolidayList([date])
        Assert.AreEqual(Nothing, result)
    End Sub
    

    然后,一旦您从失败的测试中获得值,将其插入Assert

    【讨论】:

    • 太棒了,感谢您的提示!只是一个快速澄清的问题:一旦创建了测试方法并正常工作,它是否会在执行时以类似模糊的方式自动生成随机值(在我的情况下,是用于开始日期的随机字符串),还是我应该编码生成器功能?如果后者为真,它们将如何在测试函数中使用或调用?
    • @roggmatz 除非你告诉它。通常你会给出一个特定的值并期望得到相应的结果。在您的编辑中,您有“开始和预期的待办事项”的地方是我希望设置您的起始值和相应结果的地方。
    • 如何告诉 Visual Studio 模糊函数?
    • 要么通过我在第一段中提到的模拟框架(我不确定 vb 有什么 tbh),但这通常需要使用 DI 方法或类似 Microsoft Fakes 的方式构建代码跨度>
    • 我不建议在单元测试中模糊或随机化任何数据。一个单元测试应该准确地测试界面的一个修复方面。我还鼓励阅读 Rober C. Martin 关于 TDD 的 Blog
    猜你喜欢
    • 2017-01-08
    • 2012-01-08
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    相关资源
    最近更新 更多