From 10fef2cb1f45d21a8132c2361a4697e4fefc45ce Mon Sep 17 00:00:00 2001 From: Sara Hentzel Date: Tue, 23 Apr 2019 13:36:27 -0500 Subject: [PATCH] Create test for accessing many to many data through id. ie articles/1/tags --- .../Controllers/TagsController.cs | 15 +++++++++ .../Acceptance/ManyToManyTests.cs | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs new file mode 100644 index 0000000000..bcfbe2ae18 --- /dev/null +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs @@ -0,0 +1,15 @@ +using JsonApiDotNetCore.Controllers; +using JsonApiDotNetCore.Services; +using JsonApiDotNetCoreExample.Models; + +namespace JsonApiDotNetCoreExample.Controllers +{ + public class TagsController : JsonApiController + { + public TagsController( + IJsonApiContext jsonApiContext, + IResourceService resourceService) + : base(jsonApiContext, resourceService) + { } + } +} diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs index 948ea442c9..3cd7461cdd 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs @@ -30,7 +30,38 @@ public ManyToManyTests(TestFixture fixture) { _fixture = fixture; } + [Fact] + public async Task Can_Fetch_Many_To_Many_Through_Id() + { + // arrange + var context = _fixture.GetService(); + var article = _articleFaker.Generate(); + var tag = _tagFaker.Generate(); + var articleTag = new ArticleTag + { + Article = article, + Tag = tag + }; + context.ArticleTags.Add(articleTag); + await context.SaveChangesAsync(); + var route = $"/api/v1/articles/{article.Id}/tags"; + // act + var response = await _fixture.Client.GetAsync(route); + + // assert + var body = await response.Content.ReadAsStringAsync(); + Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}"); + + var document = JsonConvert.DeserializeObject(body); + Assert.NotNull(document.Data); + + var tagResponse = _fixture.GetService().Deserialize(body); + Assert.NotNull(tagResponse); + Assert.Equal(tag.Id, tagResponse.Id); + Assert.Equal(tag.Name, tagResponse.Name); + + } [Fact] public async Task Can_Fetch_Many_To_Many_Through_All() {