Click here to Skip to main content
15,851,915 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionHelp! My .NET to MySQL application suddenly stopped working... Pin
erikbrannlund25-Aug-23 1:07
professionalerikbrannlund25-Aug-23 1:07 
AnswerRe: Help! My .NET to MySQL application suddenly stopped working... Pin
Dave Kreskowiak25-Aug-23 4:39
mveDave Kreskowiak25-Aug-23 4:39 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
erikbrannlund25-Aug-23 5:24
professionalerikbrannlund25-Aug-23 5:24 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
Gerry Schmitz25-Aug-23 5:30
mveGerry Schmitz25-Aug-23 5:30 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
jkirkerx24-Oct-23 12:47
professionaljkirkerx24-Oct-23 12:47 
QuestionMessage Closed Pin
17-Aug-23 1:11
YASH PATEL 202317-Aug-23 1:11 
QuestionRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 1:17
mveRichard MacCutchan17-Aug-23 1:17 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard Deeming17-Aug-23 2:25
mveRichard Deeming17-Aug-23 2:25 
GeneralRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 2:35
mveRichard MacCutchan17-Aug-23 2:35 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Dave Kreskowiak17-Aug-23 4:13
mveDave Kreskowiak17-Aug-23 4:13 
GeneralRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 4:23
mveRichard MacCutchan17-Aug-23 4:23 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
jschell17-Aug-23 6:37
jschell17-Aug-23 6:37 
QuestionHow would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202314-Aug-23 14:13
Nikol Dimitrova 202314-Aug-23 14:13 
Here's my Program.cs file (I've actually simplified it to some extent).

using StudentsMinimalApi;
using StudentsMinimalApi.Validation;
using System.Collections.Concurrent;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddProblemDetails();

WebApplication app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler();
}

app.UseStatusCodePages();

ConcurrentDictionary<string, Student> _students = new();

RouteGroupBuilder studentsApi = app.MapGroup("/student");

studentsApi.MapGet("/", () => _students);

RouteGroupBuilder studentsApiWithValidation = studentsApi
    .MapGroup("/"); //I use a filter factory on this builder, but it does not matter for my question.

studentsApiWithValidation.MapGet("/{id}", (string id) =>
    _students.TryGetValue(id, out var student)
       ? TypedResults.Ok(student)
       : Results.Problem(statusCode: 404));

studentsApiWithValidation.MapPost("/{id}", (Student student, string id) =>
  _students.TryAdd(id, student)
     ? TypedResults.Created($"/student/{id}", student)
     : Results.ValidationProblem(new Dictionary<string, string[]>
  {
    { "id", new[] { "A student with the given id already exists." } }
  }));

app.Run();

public partial class Program { }


As you can see, I created an example minimal API that exposes endpoints that you can use to access or change data related to an example Student class using the HTTP protocol.

So now I'd like to test my minimal API using xUnit. That's how I decided to test if the post method successfully creates a new student.

[Fact]
public async Task MapPost_Should_Successfully_Create_A_New_Student()
{
    await using var application = new WebApplicationFactory<Program>();

    using HttpClient? client = application.CreateClient();

    HttpResponseMessage? resultFromPost = await client.PostAsJsonAsync("/student/s1", new Student("X", "Y", "Z"));
    HttpResponseMessage? resultFromGet = await client.GetAsync("/student/s1");

    Assert.Equal(HttpStatusCode.Created, resultFromPost.StatusCode);
    Assert.Equal(HttpStatusCode.OK, resultFromGet.StatusCode);

    string? contentAsString = await resultFromGet.Content.ReadAsStringAsync();

    var contentAsStudentObject =
        JsonConvert.DeserializeObject<Student>(contentAsString);

    Assert.Equal(HttpStatusCode.Created, resultFromPost.StatusCode);
    Assert.Equal(HttpStatusCode.OK, resultFromGet.StatusCode);

    Assert.NotNull(contentAsStudentObject);

    Assert.Equal("X", contentAsStudentObject.FirstName);
    Assert.Equal("Y", contentAsStudentObject.LastName);
    Assert.Equal("Z", contentAsStudentObject.FavouriteSubject);
}


May I ask you if this way of testing is adequate? For the testing of the post method I've actually used the get method. This does not seem like a good approach, but I don't see how else I can handle the situation. So I can't compe up with another alternative that's maybe better than this. Is there a way for me to access the _students dictionary from the Program.cs file? How would you write your tests in this situation? Thank you in advance!
AnswerRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
jschell15-Aug-23 5:36
jschell15-Aug-23 5:36 
GeneralRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202315-Aug-23 8:46
Nikol Dimitrova 202315-Aug-23 8:46 
AnswerRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Gerry Schmitz15-Aug-23 5:37
mveGerry Schmitz15-Aug-23 5:37 
QuestionCreating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx27-Jul-23 10:08
professionaljkirkerx27-Jul-23 10:08 
AnswerRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Richard Andrew x6427-Jul-23 11:55
professionalRichard Andrew x6427-Jul-23 11:55 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx27-Jul-23 12:14
professionaljkirkerx27-Jul-23 12:14 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Dave Kreskowiak27-Jul-23 19:02
mveDave Kreskowiak27-Jul-23 19:02 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx28-Jul-23 7:26
professionaljkirkerx28-Jul-23 7:26 
QuestionNuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 7:25
professionalRichard Andrew x642-Jul-23 7:25 
AnswerRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 8:38
mveGerry Schmitz2-Jul-23 8:38 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 9:36
professionalRichard Andrew x642-Jul-23 9:36 
GeneralRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 12:03
mveGerry Schmitz2-Jul-23 12:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.