Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unreliable unit test #16145

Merged
merged 5 commits into from
May 25, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ public async Task ShouldListBlogPostWhenCallingAQuery()
.WithField("displayText");
});

var nodes = result["data"]["recentBlogPosts"];
var jsonArray = result["data"]?["recentBlogPosts"]?.AsArray();

Assert.Equal(2, nodes.AsArray().Count);
Assert.Equal("Some sorta blogpost in a Query!", nodes[0]["displayText"].ToString());
Assert.Equal("Man must explore, and this is exploration at its greatest", nodes[1]["displayText"].ToString());
Assert.NotNull(jsonArray);
Assert.Equal(2, jsonArray.Count);

// The RecentBlogPosts query sorts the content items by CreatedUtc. If the
// test is executing too fast, both blog entries may have the same CreatedUtc
// value and ordering becomes random. Because of this, we do not assert the order
// of the result.
Assert.Contains("Some sorta blogpost in a Query!", jsonArray.Select(node => node["displayText"]?.ToString()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you save the result of jsonArray.Select(node => node["displayText"]?.ToString()) into a variable and use it in both lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done!

Assert.Contains("Man must explore, and this is exploration at its greatest", jsonArray.Select(node => node["displayText"]?.ToString()));
}
}
}