C
C#2y ago
friedice

❔ Issue with unit testing and projects not starting

I'm writing up tests for my controller, but I need to grab an access token for authorization to do anything. We're using OIDC and it's endpoint ishttps://localhost:5001
public async Task < string > GetAccessToken() {
var client = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/connect/token");
request.Content = new FormUrlEncodedContent(new Dictionary < string, string > {
///stuff here
});

var response = await client.SendAsync(request);

if (!response.IsSuccessStatusCode) {
throw new Exception("Failed to obtain token");
}

var responseContent = await response.Content.ReadAsStringAsync();
var tokenResponse = JsonConvert.DeserializeObject < TokenResponse > (responseContent);

return tokenResponse.AccessToken;
}
public async Task < string > GetAccessToken() {
var client = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:5001/connect/token");
request.Content = new FormUrlEncodedContent(new Dictionary < string, string > {
///stuff here
});

var response = await client.SendAsync(request);

if (!response.IsSuccessStatusCode) {
throw new Exception("Failed to obtain token");
}

var responseContent = await response.Content.ReadAsStringAsync();
var tokenResponse = JsonConvert.DeserializeObject < TokenResponse > (responseContent);

return tokenResponse.AccessToken;
}
When I run this function in test explorer, I get a System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:5001) However, when I open a separate instance of VS and run the application, then run test on the 1st VS, everything works fine. How do I make it so that I don't need to do all the shenanigans and just run the test from the test explorer?
5 Replies
friedice
friediceOP2y ago
public class AppMenuItemController_Test : IClassFixture<AuthService>
{
private readonly IAuthService _authService;
private string _accessToken;

public AppMenuItemController_Test(AuthService authService)
{
_authService = authService;
}

[Fact]
public async void ShouldGetAccessToken()
{
_accessToken = await _authService.GetAccessToken();
Assert.True(!string.IsNullOrEmpty(_accessToken));
}
}
public class AppMenuItemController_Test : IClassFixture<AuthService>
{
private readonly IAuthService _authService;
private string _accessToken;

public AppMenuItemController_Test(AuthService authService)
{
_authService = authService;
}

[Fact]
public async void ShouldGetAccessToken()
{
_accessToken = await _authService.GetAccessToken();
Assert.True(!string.IsNullOrEmpty(_accessToken));
}
}
The code itself is fine, just having issues with my test not running all the startup project
Pobiega
Pobiega2y ago
I don't see your test code starting a web service. You'll want to read up on WebApplicationFactory
friedice
friediceOP2y ago
I'll look into it, thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server