❔ Proper mocking for Unit Testing (NUnit)

public class TargetServiceSetEndDateAsTwoMonthsOut : PreProcessorAction<CreateTargetServiceCommand>
{
private readonly IDateTime _dateTime;
public TargetServiceSetEndDateAsTwoMonthsOut(IDateTime datetime)
{
this._dateTime = datetime;
}

public override Task<bool> CanProcessAsync(CreateTargetServiceCommand command)
{
if (command != null && command.ServiceType == ServiceType.STR)
return Task.FromResult(true);
else
return Task.FromResult(false);
}

public override async Task ProcessAsync(CreateTargetServiceCommand command)
{
command.EndDate = DateOnly.FromDateTime(this._dateTime.Now).AddMonths(2);
await Task.CompletedTask;
}
}
[Test]
public async Task TargetServiceSetStartDateAsToday_EqualProcessTest()
{
var mockRepo = new Mock<IDateTime>();
var rule = new TargetServiceSetStartDateAsToday(mockRepo.Object);
var command = GetCommandWithReferenceTarget();
await rule.ProcessAsync(command);
Assert.AreEqual(command.StartDate, DateOnly.FromDateTime(mockRepo.Object.Now));
}
public class TargetServiceSetEndDateAsTwoMonthsOut : PreProcessorAction<CreateTargetServiceCommand>
{
private readonly IDateTime _dateTime;
public TargetServiceSetEndDateAsTwoMonthsOut(IDateTime datetime)
{
this._dateTime = datetime;
}

public override Task<bool> CanProcessAsync(CreateTargetServiceCommand command)
{
if (command != null && command.ServiceType == ServiceType.STR)
return Task.FromResult(true);
else
return Task.FromResult(false);
}

public override async Task ProcessAsync(CreateTargetServiceCommand command)
{
command.EndDate = DateOnly.FromDateTime(this._dateTime.Now).AddMonths(2);
await Task.CompletedTask;
}
}
[Test]
public async Task TargetServiceSetStartDateAsToday_EqualProcessTest()
{
var mockRepo = new Mock<IDateTime>();
var rule = new TargetServiceSetStartDateAsToday(mockRepo.Object);
var command = GetCommandWithReferenceTarget();
await rule.ProcessAsync(command);
Assert.AreEqual(command.StartDate, DateOnly.FromDateTime(mockRepo.Object.Now));
}
2 Replies
LotusIsWorking
LotusIsWorkingOP3y ago
Which test is better?
[Test]
public async Task TargetServiceSetStartDateAsToday_EqualProcessTest()
{
var mockRepo = new Mock<IDateTime>();
mockRepo.Setup(x => x.Now).Returns(DateTime.UtcNow);
var rule = new TargetServiceSetStartDateAsToday(mockRepo.Object);
var command = GetCommandWithReferenceTarget();
await rule.ProcessAsync(command);
Assert.AreEqual(command.StartDate, DateOnly.FromDateTime(mockRepo.Object.Now));
}
[Test]
public async Task TargetServiceSetStartDateAsToday_EqualProcessTest()
{
var mockRepo = new Mock<IDateTime>();
mockRepo.Setup(x => x.Now).Returns(DateTime.UtcNow);
var rule = new TargetServiceSetStartDateAsToday(mockRepo.Object);
var command = GetCommandWithReferenceTarget();
await rule.ProcessAsync(command);
Assert.AreEqual(command.StartDate, DateOnly.FromDateTime(mockRepo.Object.Now));
}
Accord
Accord3y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?