How do I unit test a class that depends on TaskCompletionSource? - c#

I have a class that depends on TaskCompletionSource
An example of the class looks like this:
public ExampleClass
{
private TaskCompletionSource<string> _tcs;
public async Task<string> GetFooAsync()
{
_tcs = new TaskCompletionSource<string>();
return await _tcs.Task;
}
public void SetFoo(string value)
{
_tcs.SetResult(value);
}
}
I am using xUnit.net as my testing framework.
[Fact]
public async Task ReturnsString()
{
// Arrange
const string value = "test";
var myclass = new ExampleClass();
// Act -- Does not work. I don't know how to fix this.
var result = await myclass.GetFooAsync(); // Won't return before SetFoo is called
myclass.SetFoo(value); // Needs to be run after GetFooAsync is called
// Assert
Assert.Equal(value, result);
}
(see comments in code)

For this example case, the test needs to be arranged differently
[Fact]
public async Task ReturnsString() {
// Arrange
const string expected = "test";
var sut = new ExampleClass();
var task = sut.GetFooAsync(); // launch tack and do not await
sut.SetFoo(expected); // set expected value after GetFooAsync is called
// Act
var actual = await task;
// Assert
Assert.Equal(expected, actual);
}
The task can be launched and not awaited to allow for the sut to be able to set the task result.
Once the result is set, the task can then be awaited as intended to verify the expected behavior

Related

Unable to verify method called via Xunit Mock

Using Moq 4.18.1 to test my C# code, I'm wanting to verify a method calls another method. In the class being tested I have this:
public class IntegratedPlatformRepository : PvRepository<OutputDTO>, IIntegratedPlatformRepository
{
protected virtual async Task IpSpecificGetterExtras(OutputDTO dto, CancellationToken cancellationToken) { }
public async Task<OutputDTO> GetAsync(Guid uid, CancellationToken cancellationToken = default) {
var dto = ...
await IpSpecificGetterExtras(dto, cancellationToken);
...
}
I want to ensure that IpSpecificGetterExtras is called when GetAsync is called, so I tried this:
[Fact]
public async Task GetAsync_Calls_IpSpecificGetterExtras()
{
// Arrange
var mock = new Mock<IntegratedPlatformRepository> {
CallBase = true
};
// Act
await _repo.GetAsync(Guid.NewGuid().ToString());
// Assert
mock
.Protected()
.Verify<Task>(
"IpSpecificGetterExtras", Times.Once(),
ItExpr.IsAny<OutputDTO>(), ItExpr.IsAny<CancellationToken>()
);
}
If I simply run the test it fails saying the invocation wasn't performed. If I debug the test and set a breakpoint in IpSpecificGetterExtras the breakpoint is hit and I can step through the method, so it's definitely being called.
Based on the shown example mock was not invoked.
The test needs to be refactored to allow it to flow to completion so that it can be verified as expected.
[Fact]
public async Task GetAsync_Calls_IpSpecificGetterExtras() {
// Arrange
var mock = new Mock<IntegratedPlatformRepository> {
CallBase = true
};
mock.Protected()
.Setup<Task>("IpSpecificGetterExtras",
ItExpr.IsAny<OutputDTO>(), ItExpr.IsAny<CancellationToken>())
.Returns(Task.CompletedTask);
var subject = mock.Object;
// Act
await subject.GetAsync(Guid.NewGuid().ToString());
// Assert
mock
.Protected()
.Verify<Task>(
"IpSpecificGetterExtras", Times.Once(),
ItExpr.IsAny<OutputDTO>(), ItExpr.IsAny<CancellationToken>()
);
}

Failed Unit Test

I have a service for adding city in DB. It works good, now I need test it. I have never worked with tests before. I'll be grateful for your recommendations. I would test case - If it isn't null - test passed(I mean if User insert anything - test is passed, if he doesn't insert data - test failed). I need just check townName null or no. It is my TestClass
[TestClass]
public partial class TownRepositoryTests
{
[TestMethod]
public async Task InsertTownName_ReturnTownName()
{
var builder = new RepositoryBuilder<TownRepository>().SetupManager();
using (builder.CreateIsolationScope())
{
var expectedTown = await TestBuilder.SetupTownNameAsync(builder);
var repository = builder.Build();
var townName = expectedTown.Name;
var result = await repository.InsertTown(townName);
Assert.IsNotNull(result);
}
}
}
SetupTownNameAsync method(here I add data). I think it works good, because when I used debug test, I checked that this method return (id=1, name= "TestTown")
public static class TestBuilder
{
public static async Task<Town> SetupTownNameAsync(RepositoryBuilder<TownRepository> builder)
{
var town = await builder.CreateTownAsync(name: "TestTown");
var setupResult = new Town
{
Id = town.Id,
Name = town.Name
};
return setupResult;
}
}
And here my InsertTown method
public async Task<int> InsertTown(string townName)
{
var parameters = new { townName };
return await Connection.QueryFirstOrDefaultAsync<int>(Adhoc["AddTown"], parameters, Transaction);
}
When I checked problem with debug test - in this line returns "TestTown", but when I check it in Assert - my test is failed
var result = await repository.InsertTown(townName);
Assert.IsNotNull(result);

NSubstitute returning NullReferenceException when Using ForPartsOf and mocking an Async Method

I have two methods:
public class MyClass
{
public virtual async Task MethodOne(MyModel myModel)
{
await Task.Delay(1);
throw new Exception("Test");
}
public async Task MethodTwo()
{
MyModel myModel = new MyModel();
await MethodOne(myModel);
}
}
It doesn't matter what MyModel is, but it does matter that it's a parameter.
And the test:
[Fact]
public async Task Test6()
{
// Arrange
MyClass myClass = Substitute.ForPartsOf<MyClass>();
myClass.When(async a => await a.MethodOne(Arg.Any<MyModel>())).DoNotCallBase();
// Act
await myClass.MethodTwo();
// Assert
}
The test gives a NullReferenceException when the line:
myClass.When(async a => await a.MethodOne(Arg.Any<MyModel>())).DoNotCallBase();
My guess is that it is, in some way, trying to resolve MyModel; however, the same test works, when performed on a synchronous method, or one without a complex parameter.
Can anyone tell me why this error occurs in this way?
Do not await the setup
overridden method needs to return a Task to allow async to flow to completion when invoked in test.
That means the setup needs to be rewritten
[Fact]
public async Task Test6() {
// Arrange
var myClass = Substitute.ForPartsOf<MyClass>();
myClass.MethodOne(Arg.Any<MyModel>()).Returns(Task.FromResult((object)null));
// Act
await myClass.MethodTwo();
// Assert
//...
}

Getting result from Func<Task<T>>

Method Under Test
protected override async Task<Name> DoExecuteAsync(NameContext context)
{
context.ThrowIfNull("context");
var request = new Request
{
Id = context.Id,
Principal = context.UserPrincipal,
};
return await this.repository.NameAsync(request, new CancellationToken(), context.ControllerContext.CreateLoggingContext());
}
protected override Name HandleError(NameContext viewContext, Exception exception)
{
if (this.errorSignaller != null)
{
this.errorSignaller.SignalFromCurrentContext(exception);
}
return Name.Unknown;
}
This is implementation of
public abstract class BaseQueryAsync<TInput, TOutput> : IQueryAsync<TInput, TOutput>
{
public async Task<TOutput> ExecuteAsync(TInput context)
{
try
{
return await this.DoExecuteAsync(context);
}
catch (Exception e)
{
return this.HandleError(context, e);
}
}
protected abstract Task<TOutput> DoExecuteAsync(TInput context);
protected virtual TOutput HandleError(TInput viewContext, Exception exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
}
Test Case goes like below
[SetUp]
public void Setup()
{
var httpContext = MvcMockHelpers.MockHttpContext(isAuthenticated: true);
this.controller = new Mock<Controller>();
this.controller.Object.SetMockControllerContext(httpContext.Object);
this.repoMock = new Mock<IRepository>();
this.errorSignaller = new Mock<IErrorSignaller>();
this.query = new NameQuery(this.repoMock.Object, this.errorSignaller.Object);
this.userPrinciple = new Mock<IPrincipal>();
this.context = new NameContext(this.controller.Object.ControllerContext, this.userPrinciple.Object);
}
[Test]
public async Task TestDoExecuteAsyncWhenRepositoryFails()
{
// Arrange
this.repoMock.Setup(
x => x.NameAsync(
It.IsAny<Request>(),
It.IsAny<CancellationToken>(),
It.IsAny<ILoggingContext>())).Throws<Exception>();
// Act
Func<Task<Name>> act = async () => await this.query.ExecuteAsync(this.context);
// Assert
act.ShouldNotThrow();
this.errorSignaller.Verify(s => s.SignalFromCurrentContext(It.IsAny<Exception>()), Times.Once);
}
To verify the Name Object ,When I use the var result = await act() before the line
this.errorSignaller.Verify(s => s.SignalFromCurrentContext(It.IsAny<Exception>()), Times.Once);
The this.errorSignaller.Verify fails since it's count is 2 instead of 1. My intention is to check the Name object along with below code.
act.ShouldNotThrow();
this.errorSignaller.Verify(s => s.SignalFromCurrentContext(It.IsAny<Exception>()), Times.Once);
I knew that if I write a new test case I can easily verify it, but is there any way I can do altogether in this test?
If you want to test the result then use:
Name result = await this.query.ExecuteAsync(this.context);
result.Should().Be(expectefResult);
Make sure to make your test method public async Task
Update
To be able to verify name you would need to set it in the function.
//...code removed for brevity
Name expectedName = Name.Unknown;
Name actualName = null;
// Act
Func<Task> act = async () => {
actualName = await this.query.ExecuteAsync(this.context);
};
// Assert
act.ShouldNotThrow();
actualName
.Should().NotBeNull()
.And.Be(expectedName);
//...rest of code
Original
As already mentioned in the comments, act is a function that returns a Task.
While its implementation is awaited, the function itself still needs to be invoked. And since the function returns a Task it too would need to be awaited.
Func<Task<Name>> act = async () => await this.query.ExecuteAsync(this.context);
var name = await act();
It is the same as having the following function.
async Task<Name> act() {
return await this.query.ExecuteAsync(this.context);
}
You would have to await it the same way
var name = await act();
The only difference being that the former example has the function in a delegate.
Try to avoid mixing blocking calls like .Result with async/await code. This tends to cause deadlocks.
You can try to check it with
await query.ExecuteAsync(this.context);
or
this.query.ExecuteAsync(this.context).GetAwaiter().GetResult();
and in case of Func:
act.Invoke().GetAwaiter().GetResult();

NUnit testing of Async Task fails in C#

I have the following class and the interface
public interface IService
{
Task<double> GetAccDetails(int personId);
}
public class Person
{
private int _personId;
private IService _service;
public Person(int personId, IService service)
{
_personId= personId;
_service = service;
}
public double Amount {get; set;}
public async void UpdateBanckingAcc()
{
Amount = await _service.GetAccDetails(_personId);
}
}
I am trying to write nunit test for it:
[Test]
public async void Test1([Values(200)]int personId)
{
const double expectedResult = 20;
var serviceMock = new Mock<IAccountService>();
//Here I tried both options:
//serviceMock.Setup(s => s.GetAccDetails(It.Is<int>(id => id == personId)))
// .ReturnsAsync(() => expectedResult);
//And:
serviceMock.Setup(s=> s.GetAccDetails(It.Is<int>(id => id == personId)))
.Returns(() => Task.FromResult<double>(personId));
var person = new Person(personId, serviceMock.Object);
person.UpdateBanckingAcc();
double res = person.Amount;
Assert.AreEqual(expectedResult, res);
}
And the test fails. For some strange reason I can not debug it.
So the issue I see here is the call :
person.UpdateBanckingAcc();
it should be
await person.UpdateBanckingAcc();
but it does not like if I use await keyword.
Please advise.
Also one more question: is there something specific in terms of nunit testing for async methods I should test, like task status testing, etc?
The problem here is that your method UpdateBankingAcc has return type void which should be returning a Task<T> or Task, so you need to change the signatures of it to reutrn a Task like:
public async Task UpdateBanckingAcc()
{
Amount = await _service.GetAccDetails(_personId);
}
and now you would need to change your test code to be:
await person.UpdateBanckingAcc();
double res = person.Amount;
Assert.AreEqual(expectedResult, res);
you should never return void from an async method, unless it is UI controls events, you can read about the best practices of using async and await at following :
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
http://www.tonicodes.net/blog/why-you-should-almost-never-write-void-asynchronous-methods/
async/await - when to return a Task vs void?
There is a simple rule: async void is used for fire-and-forget behavior and only for this. If you need async method, change it's return type to the Task, as #EhsanSajjad said. This corresponds to the unit tests too:
public async Task UpdateBanckingAcc()
public async Task Test1([Values(200)]int personId)

Categories

Resources