I have a really weird pair of test utilizing Flurl and xUnit that, when Run All in VS Test Explorer, will fail, but if run individually, will pass. I cannot for the life of me see anywhere where the 2 are even related to each other, but they do.
I have extracted them out of my project into a new project and the problem persists. I bundled them into a 7z for anyone interested in loading it to VS, but the full code follows.
Project.Commons
GetApi1:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
public class GetApi1
{
public async Task<string> ExecuteAsync(string token)
{
string apikeyKeyname = "token";
dynamic response = await "http://www.api.com"
.SetQueryParams(new { token = token })
.GetJsonAsync();
string receivedApiKey = ((IDictionary<string, object>)response)[apikeyKeyname].ToString();
return receivedApiKey;
}
}
}
GetApi2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace Project.Commons
{
public class GetApi2
{
public async Task<IList<string>> ExecuteAsync()
{
var responses = await "http://www.api.com"
.GetJsonAsync<List<string>>();
var result = new List<string>();
foreach (var response in responses)
{
result.Add("refined stuff");
}
return result;
}
}
}
Project.Tests
UnitTest1:
using Project.Commons;
namespace Project.Tests
{
public class UnitTest1
{
private ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public async Task ShouldBeAbleToGetApiKeyFromToken()
{
// Arrange
using (var httpTest = new HttpTest())
{
var jsonResponse = new { token = "abcdef" };
string expectedApiKey = "abcdef";
httpTest.RespondWithJson(jsonResponse);
var api = new GetApi1();
// Act
var receivedApiKey = await api.ExecuteAsync("mockToken");
output.WriteLine("Received apikey = " + receivedApiKey);
// Assert
Assert.Equal(expectedApiKey, receivedApiKey);
}
}
}
}
UnitTest2
using Flurl.Http.Testing;
using Project.Commons;
using Xunit;
using Xunit.Abstractions;
namespace Project.Tests
{
public class UnitTest2
{
#region Mock API JSON Response
private IList<string> mockResponse = new List<string>()
{
"raw stuff", "raw stuff", "raw stuff"
};
#endregion
#region Expected Result
private IList<string> expectedResult = new List<string>()
{
"refined stuff", "refined stuff", "refined stuff"
};
#endregion
[Fact]
public async Task CanGetProjectsByWeek()
{
// Arrange
using (var httpTest = new HttpTest())
{
httpTest.RespondWithJson(mockResponse);
// Act
var api = new GetApi2();
var actualResult = await api.ExecuteAsync();
// Assert
Assert.Equal(expectedResult,actualResult);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire