rspec/rules/S6354/csharp/rule.adoc

85 lines
1.8 KiB
Plaintext

include::../description.adoc[]
== Noncompliant Code Example
[source,csharp]
----
public class Foo
{
public string HelloTime() =>
$"Hello at {DateTime.UtcNow}";
}
----
== 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,csharp]
----
public interface IClock
{
DateTime UtcNow();
}
public class Foo
{
public string HelloTime(IClock clock) =>
$"Hello at {clock.UtcNow()}";
}
public class FooTest
{
public record TestClock(DateTime now) : IClock
{
public DateTime UtcNow() => now;
}
[Fact]
public void HelloTime_Gives_CorrectTime()
{
var dateTime = new DateTime(2017, 06, 11);
Assert.Equal((new Foo()).HelloTime(new TestClock(dateTime)), $"Hello at {dateTime}");
}
}
----
Another possible solution is using an adaptable static class, 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,csharp]
----
public static class Clock
{
public static DateTime UtcNow() { /* ... */ }
public static IDisposable SetTimeForCurrentThread(Func<DateTime> time) { /* ... */ }
}
public class Foo
{
public string HelloTime() =>
$"Hello at {Clock.UtcNow()}";
}
public class FooTest
{
[Fact]
public void HelloTime_Gives_CorrectTime()
{
var dateTime = new DateTime(2017, 06, 11);
using (Clock.SetTimeForCurrentThread(() => dateTime))
{
Assert.Equal((new Foo()).HelloTime(), $"Hello at {dateTime}");
}
}
}
----
== See
https://nodatime.org/3.0.x/api/NodaTime.Testing.html[NodaTime testing]