C
C#17mo 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
friedice17mo 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
Pobiega17mo ago
I don't see your test code starting a web service. You'll want to read up on WebApplicationFactory
friedice
friedice17mo ago
I'll look into it, thanks
Accord
Accord17mo 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
More Posts
❔ Retrieve API data in C#What structure would you use if you want to create an API that retrieves data from another API and r❔ HttpRequestException: No Such Host is known. (url.com:443) using WorkerService issueGreetings! I'm getting this error when my Win Service attempts to connect to my endpoint. It throws ✅ api response 401hey, I'm trying to use nlpcloud to translate text there is curl example: ```cpp curl "https://api.nl✅ warning MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible.I'm doing some codes in visual code and I don't what's wrong exactly. So the thing is I did a localh❔ Is there any way to create a pointer to a C# generic classI have a script that uses a class "fogGrid<bool>". At the start of the script I want it to be able t❔ need help with homework (while loop)hey guys, I am a novice programmer I currently go to school for an MSIS degree I am learning c# I h❔ Recursion Method Psuedocodehow do i recursively sort both sublists at the bottom? in pseudocode they are written left:=MergeSo✅ Code ReviewI'm a little worried I am not following SOLID and best practices to the best ability: https://www.re❔ How to build a function that return TextFragmentCollection based on PDF file url?I already have a searchable pdf, but I want to convert it into pdf with digital text...I already got❔ Entity Framework 7 - Where to call EnsureCreared methodI'm trying to create a PGSQL database with a code-first approach using EF7 in my ASP.NET Core Web AP