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

.NET (Core and Framework)

 
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 
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 10:55
professionalRichard Andrew x6427-Jul-23 10: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 11:14
professionaljkirkerx27-Jul-23 11: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 18:02
mveDave Kreskowiak27-Jul-23 18: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 6:26
professionaljkirkerx28-Jul-23 6:26 
QuestionNuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 6:25
professionalRichard Andrew x642-Jul-23 6:25 
AnswerRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 7:38
mveGerry Schmitz2-Jul-23 7:38 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 8:36
professionalRichard Andrew x642-Jul-23 8:36 
GeneralRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 11:03
mveGerry Schmitz2-Jul-23 11:03 
AnswerRe: NuGet Packages and Library Files Pin
Dave Kreskowiak2-Jul-23 9:27
mveDave Kreskowiak2-Jul-23 9:27 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 10:38
professionalRichard Andrew x642-Jul-23 10:38 
AnswerRe: NuGet Packages and Library Files Pin
Mirza Ahmed Ali Baig3-Sep-23 23:30
Mirza Ahmed Ali Baig3-Sep-23 23:30 
QuestionIsolated/protected execution environment? Pin
GregJ728-May-23 14:16
GregJ728-May-23 14:16 
AnswerRe: Isolated/protected execution environment? Pin
Dave Kreskowiak29-May-23 3:58
mveDave Kreskowiak29-May-23 3:58 

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.