85 lines
2.0 KiB
Plaintext
85 lines
2.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,vbnet]
|
|
----
|
|
|
|
Public Class Foo
|
|
Public Function HelloTime() As String
|
|
Return $"Hello at {DateTime.UtcNow}"
|
|
End Function
|
|
End Class
|
|
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
There are different approaches to solve this problem. One of them is suggested below. There are also open source libraries (such as NodaTime) which already implement an `IClock` interface and a `FakeClock` testing class.
|
|
|
|
[source,vbnet]
|
|
----
|
|
|
|
Public Interface IClock
|
|
Function UtcNow() As Date
|
|
End Interface
|
|
|
|
Public Class Foo
|
|
Public Function HelloTime(clock As IClock) As String
|
|
Return $"Hello at {clock.UtcNow()}"
|
|
End Function
|
|
End Class
|
|
|
|
Public Class FooTest
|
|
Public Class TestClock
|
|
Implements IClock
|
|
' implement
|
|
End Class
|
|
|
|
<Fact>
|
|
Public Sub HelloTime_Gives_CorrectTime()
|
|
Dim dateTime = New DateTime(2017, 06, 11)
|
|
Assert.Equal((New Foo()).HelloTime(New TestClock(dateTime)), $"Hello at {dateTime}")
|
|
End Sub
|
|
End Class
|
|
|
|
----
|
|
|
|
Another possible solution is using an adaptable module, ideally supports an IDisposable method, that not only adjusts the time behaviour for the current thread only, but also for scope of the using.
|
|
|
|
[source,vbnet]
|
|
----
|
|
|
|
Public Module Clock
|
|
Public Function UtcNow() As Date
|
|
End Function
|
|
|
|
Public Function SetTimeForCurrentThread(time As Func(Of Date)) As IDisposable
|
|
End Function
|
|
End Module
|
|
|
|
Public Class Foo
|
|
Public Function HelloTime() As String
|
|
Return $"Hello at {Clock.UtcNow()}"
|
|
End Function
|
|
End Class
|
|
|
|
Public Class FooTest
|
|
<Fact>
|
|
Public Sub HelloTime_Gives_CorrectTime()
|
|
Dim dateTime = New DateTime(2017, 06, 11)
|
|
|
|
Using SetTimeForCurrentThread(Function() dateTime)
|
|
Assert.Equal((New Foo()).HelloTime(), $"Hello at {dateTime}")
|
|
End Using
|
|
End Sub
|
|
End Class
|
|
|
|
----
|
|
|
|
== Resources
|
|
|
|
https://nodatime.org/3.0.x/api/NodaTime.Testing.html[NodaTime testing]
|