forked from walterpinson/reference-architecture-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoriesControllerTester.cs
141 lines (114 loc) · 4.62 KB
/
CategoriesControllerTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
namespace Test.Unit.Infrastructure.WebApi.Controllers
{
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Principal;
using CompanyName.Notebook.NoteTaking.Core.Application.Messages;
using CompanyName.Notebook.NoteTaking.Core.Application.Services;
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using NSubstitute;
using NUnit.Framework;
using Test.Unit.Infrastructure.WebApi.Controllers.Bases;
[TestFixture]
public class CategoriesControllerTester : ControllerTesterTemplate<CategoriesController>
{
INoteTaker _noteTaker;
ILogger<CategoriesController> _logger;
protected override CategoriesController EstablishContext()
{
_noteTaker = Substitute.For<INoteTaker>();
_logger = Substitute.For<ILogger<CategoriesController>>();
return new CategoriesController(_noteTaker, _logger);
}
protected override void TestCleanup()
{
_noteTaker.ClearReceivedCalls();
_logger.ClearReceivedCalls();
}
[OneTimeSetUp]
public void OneTimeSetUp()
{
}
[Test]
public void CanConstructCategoriesController()
{
// ARRANGE
var noteTaker = Substitute.For<INoteTaker>();
var logger = Substitute.For<ILogger<CategoriesController>>();
// ACT
var subjectUnderTest = new CategoriesController(noteTaker, logger);
// ASSERT
Assert.That(subjectUnderTest, Is.Not.Null);
Assert.That(subjectUnderTest, Is.TypeOf(typeof(CategoriesController)));
}
[Test]
public void CanGetAllCategories()
{
// ARRANGE
var expectedCategoryDtos = Substitute.For<IList<CategoryDto>>();
_noteTaker.ListCategories(Arg.Any<SecurityContext>()).Returns(expectedCategoryDtos);
// ACT
var result = _subjectUnderTest.Get();
// ASSERT
Assert.That(result, Is.Not.Null);
_noteTaker.Received(1).ListCategories(Arg.Any<SecurityContext>());
}
[Test]
public void CanGetCategory()
{
// ARRANGE
var expectedCategoryDto = Substitute.For<CategoryDto>();
var expectedId = Guid.NewGuid();
_noteTaker.GetCategoryDetail(Arg.Any<SecurityContext>(), expectedId).Returns(expectedCategoryDto);
// ACT
var result = _subjectUnderTest.Get(expectedId);
// ASSERT
Assert.That(result, Is.Not.Null);
_noteTaker.Received(1).GetCategoryDetail(Arg.Any<SecurityContext>(), expectedId);
}
[Test]
public void CanRenameCategory()
{
// ARRANGE
var expectedId = Guid.NewGuid();
var expectedNewName = "Red Dwarfs";
var expectedUpdatedCategoryDto = Substitute.For<CategoryDto>();
expectedUpdatedCategoryDto.Name = expectedNewName;
_noteTaker.RenameCategory(Arg.Any<SecurityContext>(), expectedId, expectedNewName).Returns(expectedUpdatedCategoryDto);
// ACT
var result = _subjectUnderTest.Put(expectedId, expectedUpdatedCategoryDto);
// ASSERT
Assert.That(result, Is.Not.Null);
_noteTaker.Received(1).RenameCategory(Arg.Any<SecurityContext>(), expectedId, expectedNewName);
}
[Test]
public void CanCreateCategory()
{
// ARRANGE
var newCategoryMessage = Substitute.For<NewCategoryMessage>();
var expectedCategoryDto = Substitute.For<CategoryDto>();
_noteTaker.CreateNewCategory(Arg.Any<SecurityContext>(), newCategoryMessage).Returns(expectedCategoryDto);
// ACT
var result = _subjectUnderTest.Post(newCategoryMessage);
// ASSERT
Assert.That(result, Is.Not.Null);
_noteTaker.Received(1).CreateNewCategory(Arg.Any<SecurityContext>(), newCategoryMessage);
}
[Test]
public void CanDeleteCategory()
{
// ARRANGE
var expectedId = Guid.NewGuid();
// ACT
var result = _subjectUnderTest.Delete(expectedId);
// ASSERT
Assert.That(result, Is.Not.Null);
_noteTaker.Received(1).RemoveCategory(Arg.Any<SecurityContext>(), expectedId);
}
}
}