microsoftgraph / microsoftgraph/msgraph-sdk-dotnet
[Client bug]: V5 Unit test extension posts
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 789
- Forks
- 264
- Avg merge
- 15h 17m
- Merged PRs (30d)
- 3
Description
Describe the bug
Converting a project that was using V4 of the beta to V5 of the non beta.
I encountered an issue where we are testing the creation of a user. I was able to mock the creation of the user just fine but then when I tried to mock the creation of an OpenTypeExtension on the user that was just created, I got a NullReferenceException.
To Reproduce
- Mocked client
var mockRequestAdapter = new Mock<IRequestAdapter>();
var client = new GraphServiceClient(mockRequestAdapter.Object);
...
mockRequestAdapter.Setup(
adapter => adapter.SerializationWriterFactory.GetSerializationWriter(It.IsAny<string>())
).Returns(new JsonSerializationWriter());
- Mock user creation (works)
var user = new User
{
...
}
mockRequestAdapter.Setup(
adapter => adapter.SendAsync(It.IsAny<RequestInformation>(), User.CreateFromDiscriminatorValue, It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(), It.IsAny<CancellationToken>())
).ReturnsAsync(user);
- Mock extension creation (failed)
var mockExtension = new OpenTypeExtension();
var mockAdditionalData = new Dictionary<string, object>
{
...
};
mockExtension.AdditionalData = mockAdditionalData;
mockRequestAdapter.Setup(
adapter => adapter.SendAsync(It.IsAny<RequestInformation>(), OpenTypeExtension.CreateFromDiscriminatorValue, It.IsAny<Dictionary<string, ParsableFactory<IParsable>>>(), It.IsAny<CancellationToken>())
).ReturnsAsync(mockExtension);
- Run test that creates a user and then tries to add an extension
var result = await client.Users.PostAsync(user).ConfigureAwait(false); (works)
var extension = new OpenTypeExtension
{
...
};
var extensionResult = await client.Users[result.Id].Extenstions.PostAsync(extension); (fails)
Using the debugger I was able to verify that result is probably populated with mocked user response.
Throws the error once that last line is executed, result.Id and extension and properly populated.
** Error **
Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
Utf8JsonWriter.FirstCallToGetMemory(Int32 requiredSize)
Utf8JsonWriter.Grow(Int32 requiredSize)
Utf8JsonWriter.WriteStartMinimized(Byte token)
Utf8JsonWriter.WriteStartSlow(Byte token)
Utf8JsonWriter.WriteStart(Byte token)
JsonSerializationWriter.WriteObjectValue[T](String key, T value, IParsable[] additionalValuesToMerge)
RequestInformation.SetContentFromParsable[T](IRequestAdapter requestAdapter, String contentType, T item)
ExtensionsRequestBuilder.ToPostRequestInformation(Extension body, Action`1 requestConfiguration)
ExtensionsRequestBuilder.PostAsync(Extension body, Action`1 requestConfiguration, CancellationToken cancellationToken)
GraphService.CreateUserInGraph(User user) line 628
GraphService.CreateUser(User user) line 158
PostUserTests.CreateUserAndAddExtension() line 191
--- End of stack trace from previous location ---
Expected behavior
Expected await client.Users[result.Id].Extenstions.PostAsync(extension); to return the mocked extension setup in the test method.
Client version
Microsoft.Graph 5.5.0
Additional context
When actually running the app, I am able to run this code, create a user, and add the extension without problems. It seems to be how I am mocking the return value of await client.Users[result.Id].Extenstions.PostAsync(extension); that is not working.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the PostUserTests.CreateUserAndAddExtension test at line 191 and trace the mocked IRequestAdapter through ExtensionsRequestBuilder.PostAsync and RequestInformation.SetContentFromParsable. Check the JsonSerializationWriter path for OpenTypeExtension.CreateFromDiscriminatorValue and compare it with the working User mock. Done means the test posts the extension and receives the configured mockExtension without a NullReferenceException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100