- Student JSON response should have all exercises that are assigned to them if the
include=exercise
query string parameter is there. - Exercise JSON response should have all currently assigned students if the
include=students
query string parameter is there. - Provide support for each resource (Instructor, Student, Cohort, Exercise) and the
q
query string parameter. If it is provided, your SQL should search relevant property for a match, search all properties of the resource for a match.FirstName
,LastName
, andSlackHandle
for instructors and students.Name
andLanguage
for exercises.Name
for cohorts.
Hint: Use LIKE in the SQL query for pattern matching.
Name
andLanguage
properties on an exercises should be required.- Instructor
FirstName
,LastName
, andSlackHandle
should be required. - Cohort
Name
should be required. - Cohort
Name
should be a minimum of of 5 characters and and no more than 11. - Student, and Instructor
SlackHandle
value should be a minimum of 3 characters and no more than 12.
- Open your Web API solution in Visual Studio
- In the Solution Explorer, right-click the Solution to open a menu.
- Use the menu select Add / New Project to open the Add New Project dialog.
- On the left panel, click the Test node under Visual C#.
- In the center panel, click xUnit Test Project (.NET Core).
- Enter a name for your project in the Name textbox at the bottom of the form. (something like "<YOUR_WEB_API_PROJECT_NAME>.Tests", ex.
StudentExercisesAPI.Tests
)
Now we need to setup the new Test Project with a reference to your Web API project AND with the correct Nuget packages needed to perform Integration Tests. Visual Studio offers several different ways of adding these references, however, the simplest way is to edit the test project's .csproj file.
- In the Solution Explorer, right-click the Test Project and select Edit <Your_Test_Project>.csproj in the middle of the context menu.
- Add the appropriate PackageReferences to ensure you have the following references in your project.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
- Below the PackageReferencess add the following ProjectReference section to give your Test Project access to your Web API Project.
NOTE: Make sure you fill in the
__YOUR SOLUTION NAME__
and__YOUR WEB API PROJECT NAME__
placeholders with the names of your solution and project.
<ItemGroup>
<ProjectReference Include="..\__YOUR SOLUTION NAME__\__YOUR WEB API PROJECT NAME__.csproj" />
</ItemGroup>
- Create a new class in your Test Project. Call it
APIClientProvider
. Replace the code in the new file with the following code. Note: Make sure to fill in the__YOUR WEB API PROJECT NAMESPACE__
and__YOUR TEST PROJECT NAMESPACE__
placeholders.
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;
using Xunit;
using __YOUR WEB API PROJECT NAMESPACE__;
namespace __YOUR TEST PROJECT NAMESPACE__
{
class APIClientProvider : IClassFixture<WebApplicationFactory<Startup>>
{
public HttpClient Client { get; private set; }
private readonly WebApplicationFactory<Startup> _factory = new WebApplicationFactory<Startup>();
public APIClientProvider()
{
Client = _factory.CreateClient();
}
public void Dispose()
{
_factory?.Dispose();
Client?.Dispose();
}
}
}
- In the Test menu click the Window/Test Explorer item to open the Test Explorer.
Now it's time to write soem tests
- Make a new class called something like
<SOME_CONTROLLER_NAME>Tests
(ex.StudentsControllerTests
. - Add a test method to the new class.
Example
public class StudentsControllerTests
{
[Fact]
public async Task Get_All_Students_Returns_Some_Students()
{
using (HttpClient client = new APIClientProvider().Client)
{
var response = await client.GetAsync("/api/students");
response.EnsureSuccessStatusCode();
}
}
}
You need to create integration tests for the following features of your Student Exercises API.
- Basic CRUD for Students
- Basic CRUD for Cohorts
- Basic CRUD for Instructors
- Basic CRUD for Exercises