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

.NET (Core and Framework)

 
AnswerRe: Capturing a still from a RTSP stream Pin
Richard MacCutchan19-Sep-23 1:52
mveRichard MacCutchan19-Sep-23 1:52 
AnswerRe: Capturing a still from a RTSP stream Pin
jschell19-Sep-23 5:43
jschell19-Sep-23 5:43 
QuestionVB.NET Framework 7, how to multithread on existing user control? Pin
Member 1415594910-Sep-23 7:06
Member 1415594910-Sep-23 7:06 
AnswerRe: VB.NET Framework 7, how to multithread on existing user control? Pin
Dave Kreskowiak10-Sep-23 11:43
mveDave Kreskowiak10-Sep-23 11:43 
AnswerRe: VB.NET Framework 7, how to multithread on existing user control? Pin
Gerry Schmitz10-Sep-23 16:05
mveGerry Schmitz10-Sep-23 16:05 
Questionvb.net 2022 webview2 getting current url. Pin
steve20152-Sep-23 10:16
steve20152-Sep-23 10:16 
AnswerRe: vb.net 2022 webview2 getting current url. Pin
Dave Kreskowiak2-Sep-23 10:50
mveDave Kreskowiak2-Sep-23 10:50 
GeneralRe: vb.net 2022 webview2 getting current url. Pin
steve20153-Sep-23 4:08
steve20153-Sep-23 4:08 
QuestionHelp! My .NET to MySQL application suddenly stopped working... Pin
erikbrannlund25-Aug-23 0:07
professionalerikbrannlund25-Aug-23 0:07 
AnswerRe: Help! My .NET to MySQL application suddenly stopped working... Pin
Dave Kreskowiak25-Aug-23 3:39
mveDave Kreskowiak25-Aug-23 3:39 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
erikbrannlund25-Aug-23 4:24
professionalerikbrannlund25-Aug-23 4:24 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
Gerry Schmitz25-Aug-23 4:30
mveGerry Schmitz25-Aug-23 4:30 
GeneralRe: Help! My .NET to MySQL application suddenly stopped working... Pin
jkirkerx24-Oct-23 11:47
professionaljkirkerx24-Oct-23 11:47 
QuestionMessage Closed Pin
17-Aug-23 0:11
YASH PATEL 202317-Aug-23 0:11 
QuestionRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 0:17
mveRichard MacCutchan17-Aug-23 0:17 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard Deeming17-Aug-23 1:25
mveRichard Deeming17-Aug-23 1:25 
GeneralRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 1:35
mveRichard MacCutchan17-Aug-23 1:35 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Dave Kreskowiak17-Aug-23 3:13
mveDave Kreskowiak17-Aug-23 3:13 
GeneralRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
Richard MacCutchan17-Aug-23 3:23
mveRichard MacCutchan17-Aug-23 3:23 
AnswerRe: How can Power BI development services assist businesses in transforming raw data into meaningful insights and visualizations? Pin
jschell17-Aug-23 5:37
jschell17-Aug-23 5:37 
QuestionHow would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202314-Aug-23 13:13
Nikol Dimitrova 202314-Aug-23 13: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 4:36
jschell15-Aug-23 4:36 
GeneralRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Nikol Dimitrova 202315-Aug-23 7:46
Nikol Dimitrova 202315-Aug-23 7:46 
AnswerRe: How would you test a minimal api that uses a dictionary for saving entities? Pin
Gerry Schmitz15-Aug-23 4:37
mveGerry Schmitz15-Aug-23 4: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 9:08
professionaljkirkerx27-Jul-23 9:08 

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.