Skip to content

Commit 46f1e2d

Browse files
committed
test(#354): add EntityResourceService tests
1 parent 5a7d7da commit 46f1e2d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using JsonApiDotNetCore.Builders;
4+
using JsonApiDotNetCore.Data;
5+
using JsonApiDotNetCore.Services;
6+
using JsonApiDotNetCoreExample.Models;
7+
using Microsoft.Extensions.Logging;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace UnitTests.Services
12+
{
13+
public class EntityResourceService_Tests
14+
{
15+
private readonly Mock<IJsonApiContext> _jsonApiContextMock = new Mock<IJsonApiContext>();
16+
private readonly Mock<IEntityRepository<TodoItem>> _repositoryMock = new Mock<IEntityRepository<TodoItem>>();
17+
private readonly ILoggerFactory _loggerFactory = new Mock<ILoggerFactory>().Object;
18+
19+
public EntityResourceService_Tests()
20+
{
21+
_jsonApiContextMock
22+
.Setup(m => m.ContextGraph)
23+
.Returns(
24+
new ContextGraphBuilder()
25+
.AddResource<TodoItem>("todo-items")
26+
.Build()
27+
);
28+
}
29+
30+
[Fact]
31+
public async Task GetRelationshipAsync_Passes_Public_ResourceName_To_Repository()
32+
{
33+
// arrange
34+
const int id = 1;
35+
const string relationshipName = "collection";
36+
37+
_repositoryMock.Setup(m => m.GetAndIncludeAsync(id, relationshipName))
38+
.ReturnsAsync(new TodoItem());
39+
40+
var repository = GetService();
41+
42+
// act
43+
await repository.GetRelationshipAsync(id, relationshipName);
44+
45+
// assert
46+
_repositoryMock.Verify(m => m.GetAndIncludeAsync(id, relationshipName), Times.Once);
47+
}
48+
49+
[Fact]
50+
public async Task GetRelationshipAsync_Returns_Relationship_Value()
51+
{
52+
// arrange
53+
const int id = 1;
54+
const string relationshipName = "collection";
55+
56+
var todoItem = new TodoItem
57+
{
58+
Collection = new TodoItemCollection { Id = Guid.NewGuid() }
59+
};
60+
61+
_repositoryMock.Setup(m => m.GetAndIncludeAsync(id, relationshipName))
62+
.ReturnsAsync(todoItem);
63+
64+
var repository = GetService();
65+
66+
// act
67+
var result = await repository.GetRelationshipAsync(id, relationshipName);
68+
69+
// assert
70+
Assert.NotNull(result);
71+
var collection = Assert.IsType<TodoItemCollection>(result);
72+
Assert.Equal(todoItem.Collection.Id, collection.Id);
73+
}
74+
75+
private EntityResourceService<TodoItem> GetService() =>
76+
new EntityResourceService<TodoItem>(_jsonApiContextMock.Object, _repositoryMock.Object, _loggerFactory);
77+
}
78+
}

0 commit comments

Comments
 (0)