C
C#2mo ago
Vowzss

NUnit test with Asp net web API

Hello, Im having trouble to setup NUnit tests for the API, as of now it seems Ive managed to start the service and it's running yet whenever i send the data to my endpoint it simply returns a 500 message...
11 Replies
Vowzss
VowzssOP2mo ago
Here is my whole code, I can also share some of my controller code
No description
Vowzss
VowzssOP2mo ago
and basically EtherEntryPoint is the default class for the API
No description
jcotton42
jcotton422mo ago
I'm pretty sure you're not supposed to set BaseAddress. here's an example from the WAF docs
public class BasicTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;

public BasicTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
}

[Theory]
[InlineData("/")]
[InlineData("/Index")]
[InlineData("/About")]
[InlineData("/Privacy")]
[InlineData("/Contact")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
var client = _factory.CreateClient();

// Act
var response = await client.GetAsync(url);

// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
}
}
public class BasicTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;

public BasicTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
}

[Theory]
[InlineData("/")]
[InlineData("/Index")]
[InlineData("/About")]
[InlineData("/Privacy")]
[InlineData("/Contact")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
var client = _factory.CreateClient();

// Act
var response = await client.GetAsync(url);

// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
}
}
jcotton42
jcotton422mo ago
Integration tests in ASP.NET Core
Learn how integration tests ensure that an app's components function correctly at the infrastructure level, including the database, file system, and network.
jcotton42
jcotton422mo ago
@Vowzss also, in the future, do not use screenshots to share your $code
MODiX
MODiX2mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Vowzss
VowzssOP2mo ago
my bad IClassFixture is from XUnit right ?
jcotton42
jcotton422mo ago
yes, but it shouldn't matter afaik as in whether you're using xunit or not
Vowzss
VowzssOP2mo ago
atm I just want something that works xD
public class EtherIntegrationTests(WebApplicationFactory<EtherEntryPoint> factory)
: IClassFixture<WebApplicationFactory<EtherEntryPoint>>
{
[Theory]
[InlineData("/api/v1/auth/signup")]
public async Task Test_SignUp(string url)
{
var client = factory.CreateClient();
var payload = new SignUpRequestPayload
{
Username = "e",
Email = "[email protected]",
Password = "e#20515"
};

var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();

Console.WriteLine(responseString);
}
}
public class EtherIntegrationTests(WebApplicationFactory<EtherEntryPoint> factory)
: IClassFixture<WebApplicationFactory<EtherEntryPoint>>
{
[Theory]
[InlineData("/api/v1/auth/signup")]
public async Task Test_SignUp(string url)
{
var client = factory.CreateClient();
var payload = new SignUpRequestPayload
{
Username = "e",
Email = "[email protected]",
Password = "e#20515"
};

var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();

Console.WriteLine(responseString);
}
}
following the provided exemple still the same issue with it :/ I have no clue what I am doing wrong
jcotton42
jcotton422mo ago
may want to move to #web for this
Vowzss
VowzssOP2mo ago
o yeah thank you! will do
Want results from more Discord servers?
Add your server