相关疑难解决方法(0)

当所需代码几乎没有逻辑时,是否仍然使用TDD编写测试?为什么?

TDD应该具有100%的代码覆盖率.这是否意味着应该为属性getter和setter编写测试,以及其他不包含实际逻辑的方法,例如处理外部API功能?

例1:

下面是一个示例方法(这也恰好是另一个SO问题中的示例,该问题涉及如何最好地测试它,如果我们要测试它).这种方法做得不多.它System.ServiceProcess.ServiceController是停止服务功能的外观.目前这段代码不是使用TDD编写的,但如果是,那么它应该是一个应该测试的东西吗?这里的逻辑非常少.测试本身并没有那么有用.

仅供参考:如果您想回答如何最好地测试它(IoC和适配器模式与排斥),请参阅其他问题.

Public Function StopService(ByVal serviceName As String, ByVal timeoutMilliseconds As Double) As Boolean Implements IWindowsServicesService.StopService

    Try
        Dim service As New ServiceController(serviceName)
        Dim timeout As TimeSpan = TimeSpan.FromMilliseconds(timeoutMilliseconds)

        service.[Stop]()

        If timeoutMilliseconds <= 0 Then
            service.WaitForStatus(ServiceControllerStatus.Stopped)
        Else
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout)
        End If

        Return service.Status = ServiceControllerStatus.Stopped

    Catch ex As Win32Exception
        Return False
    Catch ex As TimeoutException
        Return False
    End Try

End Function
Run Code Online (Sandbox Code Playgroud)

例2:

如果有人认为代码仍然有一些逻辑,因此在进行TDD时需要进行测试,那么下面的代码没有逻辑:

Public Function GetProcess(ByVal …
Run Code Online (Sandbox Code Playgroud)

c# vb.net tdd unit-testing code-coverage

3
推荐指数
2
解决办法
685
查看次数

标签 统计

c# ×1

code-coverage ×1

tdd ×1

unit-testing ×1

vb.net ×1