You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/development/debugging.md
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ sidebar_position: 0
9
9
10
10
GraphQL will execute sibling fields asynchronously during normal operation. This includes multiple top-level controller action calls. However, during a debugging session, having multiple fields trying to resolve themselves can play havoc with your debug cursor. If you've ever encountered a situation where the yellow line in Visual Studio seemly jumps around to random lines of code then you've experienced this issue.
11
11
12
-
At startup, it can help to disable asynchronous field resolution and instead force each field to execute in sequential order awaiting its completion before beginning the next one. Don't forget to disable this in production though, as awaiting fields individually will _**significantly**_ impact performance.
12
+
At startup, it can help to disable asynchronous field resolution and instead force each field to execute in sequential order awaiting its completion before beginning the next one.
13
13
14
14
```csharp title="Configure Debug Mode"
15
15
services.AddGraphQL(options=>
@@ -18,3 +18,6 @@ services.AddGraphQL(options =>
18
18
options.ExecutionOptions.DebugMode=true;
19
19
});
20
20
```
21
+
:::danger Performance Killer
22
+
Don't forget to disable debug mode in production though. Awaiting fields individually will _**significantly**_ impact performance.
Copy file name to clipboardExpand all lines: docs/development/unit-testing.md
+124-9Lines changed: 124 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -5,43 +5,73 @@ sidebar_label: Unit Testing
5
5
sidebar_position: 1
6
6
---
7
7
8
-
GraphQL ASP.NET has more than `3500 unit tests and 91% code coverage`. Much of this is powered by a test component designed to quickly build a configurable, fully mocked server instance to perform a query. It may be helpful to download the code and extend it for harnessing your own controllers.
8
+
<spanclassName="pill">.NET 6+</span>
9
+
<br/>
10
+
<br/>
9
11
10
-
The `TestServerBuilder<TSchema>` can be found in the `graphql-aspnet-testframework` project of the primary repo and is dependent on `Moq`. As its part of the core library solution you'll want to remove the project reference to `graphql-aspnet` project and instead add a reference to the nuget package.
12
+
:::info
13
+
Your test projects must target .NET 6 or greater to use the test framework
14
+
:::
15
+
16
+
GraphQL ASP.NET has more than `3500 unit tests and 91% code coverage`. All the internal integration tests are powered by a framework designed to quickly build a configurable, fully mocked server instance to perform a query against the runtime. It may be helpful to use and extend the framework to test your own controllers.
11
17
12
18
This document explains how to perform some common test functions for your own controller methods.
13
19
20
+
21
+
## Install the Framework
22
+
23
+
Add a reference to the [Nuget Package](https://www.nuget.org/packages/GraphQL.AspNet.TestFramework)`GraphQL.AspNet.TestFramework` to your unit test project. The framework is just a class library and not dependent on any individual testing framework like NUnit or XUnit. However, it does mock some runtime only objects and, as a result, is dependent on [Moq](https://www.nuget.org/packages/Moq).
24
+
25
+
```powershell title="Install the Test Framework"
26
+
# Using the dotnet CLI
27
+
> dotnet add package GraphQL.AspNet.TestFramework
28
+
29
+
# Using Package Manager Console
30
+
> Install-Package GraphQL.AspNet.TestFramework
31
+
```
32
+
33
+
34
+
14
35
## Create a Test Server
15
36
16
-
1. Create a new instance of the `TestServerBuilder`. The builder takes in a set of flags to perform some auto configurations for common scenarios such as exposing exceptions or altering the casing of graph type names.
37
+
1. Create a new instance of the `TestServerBuilder`. The builder takes in an optional set of flags to perform some auto configurations for common scenarios such as exposing exceptions or altering the casing of graph type names.
17
38
2. Configure your test scenario
18
39
19
40
- Use `.User` to add any permissions to the mocked user account
20
41
- Use `.Authorization` to add any security policy definitions if you wish to test security
21
42
- Use `.AddGraphQL()` to mimic the functionality of schema configuration used when your application starts.
22
-
-The `TestServerBuilder` implements `IServiceCollection`, add any additional mocked services as needed to ensure your controllers are wired up correctly by the runtime.
43
+
-`TestServerBuilder` implements `IServiceCollection`. Add any additional mocked services as needed to ensure your controllers are wired up correctly by the runtime.
23
44
24
45
3. Build the server instance using `.Build()`
25
46
26
47
```csharp title="Configuring a Test Server Instance"
27
48
[Test]
28
49
publicasyncTaskMyController_InvocationTest()
29
50
{
51
+
// Arrange
30
52
varbuilder=newTestServerBuilder();
31
53
builder.AddGraphQL(o=> {
32
54
o.AddController<MyController>();
33
55
});
34
56
57
+
// Act
35
58
varserver=builder.Build();
36
-
//...
59
+
60
+
// continued...
37
61
}
38
62
39
63
```
40
64
65
+
:::tip Custom Schemas
66
+
Use `TestServerBuild<TSchema>` to test against a custom defined schema instance.
67
+
:::
68
+
41
69
## Execute a Query
42
70
43
-
1. Mock the query execution context (the object that the runtime acts on) using `.CreateQueryContextBuilder()`
44
-
2. Configure the text, variables etc. on the builder.
71
+
Follow these steps to execute a query against the runtime. If your controller is registered to the test server and the appropriate field is requested in the query, it will be invoked.
72
+
73
+
1. Create a builder to generate a mocked execution context (the object that the runtime acts on) using `.CreateQueryContextBuilder()`
74
+
2. Configure the query text, variables etc. on the builder.
45
75
3. Build the context and submit it for processing:
46
76
- Use `server.ExecuteQuery()` to process the context. `context.Result` will be filled with the final `IQueryExecutionResult` which can be inspected for resultant data fields and error messages.
47
77
- Use `server.RenderResult()` to generate the json string a client would recieve if they performed the query.
@@ -50,13 +80,20 @@ public async Task MyController_InvocationTest()
@@ -71,5 +108,83 @@ public async Task MyController_InvocationTest()
71
108
}
72
109
*/
73
110
}
111
+
```
112
+
113
+
## Other Test Scenarios
114
+
115
+
### Throwing Exceptions
116
+
If you need to test that your controller throws an appropriate exception you can inspect the response object (instead of rendering a result). The exception will be attached to an error message generated during the query execution.
Use `server.ExecuteQuery` to obtain a reference to the response object. This allows you to interrogate the message data before its rendered as a json document.
149
+
:::
150
+
151
+
152
+
### Authn & Authz
153
+
154
+
Test authentication and authorization scenarios by configuring both the policies the server should support and the claims or roles the user will present during the test.
The user context is always injected when you run a query on the test server. By default it is an anonymous user and credentials are applied when you add a claim or policy to the context during setup.
A bitwise flag enumeration that controls which actions the multi-part request extension. By default, both batch queries and file uploads are enabled.
451
+
A bitwise flag enumeration that controls which parts of the multi-part request extension are enabled. By default, both batch queries and file uploads are enabled.
Determines if, when starting up the application, the extension will check that the required http processor is registered. When set to true, if the required processor is not registered and configuration exception will be thrown and the server will fail to start. This can be helpful when registering multiple extensions to ensure that a valid processor is registered such that multipartform requests will be handled correctly.
511
+
Determines if, when starting up the application, the extension will check that the required http processor is registered. When set to true, if the required processor is not registered a configuration exception will be thrown and the server will fail to start. This can be helpful when registering multiple extensions to ensure that a valid processor is registered such that multipart-form requests will be handled correctly.
512
512
513
513
## Demo Project
514
514
See the [demo projects](../reference/demo-projects.md) for a sample project utilizing [jaydenseric's apollo-upload-client](https://github.com/jaydenseric/apollo-upload-client) as a front end for performing file uploads against this extension.
0 commit comments