Skip to content

Commit 5907778

Browse files
authored
Merge pull request #6524 from erikjanwestendorp/update-unit-testing
Porting old Umbraco API Controller [Unit Testing]
2 parents c35b860 + 7a6b563 commit 5907778

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

14/umbraco-cms/implementation/unit-testing.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,21 @@ public class PageSurfaceControllerTests
159159
`ServiceContext.CreatePartial()` has optional parameters, and by naming them you only need to mock the dependencies that you need, for example: `ServiceContext.CreatePartial(contentService: Mock.Of<IContentService>());`
160160
{% endhint %}
161161

162-
## Testing an UmbracoApiController
162+
## Testing a Controller
163163

164164
See [Reference documentation on UmbracoApiControllers](../reference/routing/umbraco-api-controllers/README.md#locally-declared-controller).
165165

166-
{% hint style="warning" %}
167-
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
168-
{% endhint %}
169-
170166
```csharp
171-
public class ProductsController : UmbracoApiController
167+
[ApiController]
168+
[Route("/umbraco/api/products")]
169+
public class ProductsController : Controller
172170
{
173171
public IEnumerable<string> GetAllProducts()
174172
{
175173
return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
176174
}
177175

178-
[HttpGet]
176+
[HttpGet("getallproductsjson")]
179177
public JsonResult GetAllProductsJson()
180178
{
181179
return new JsonResult(this.GetAllProducts());
@@ -199,15 +197,15 @@ public class ProductsControllerTests
199197

200198
var result = this.controller.GetAllProducts();
201199

202-
Assert.AreEqual(expected, result);
200+
Assert.That(expected == result);
203201
}
204202

205203
[Test]
206204
public void WhenGetAllProductsJson_ThenReturnViewModelWithExpectedJson()
207205
{
208-
var json = serializer.Serialize(this.controller.GetAllProductsJson().Value);
206+
var json = JsonSerializer.Serialize(this.controller.GetAllProductsJson().Value);
209207

210-
Assert.AreEqual("[\"Table\",\"Chair\",\"Desk\",\"Computer\",\"Beer fridge\"]", json);
208+
Assert.That("[\"Table\",\"Chair\",\"Desk\",\"Computer\",\"Beer fridge\"]" == json);
211209
}
212210
}
213211
```

15/umbraco-cms/implementation/unit-testing.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,21 @@ public class PageSurfaceControllerTests
159159
`ServiceContext.CreatePartial()` has optional parameters, and by naming them you only need to mock the dependencies that you need, for example: `ServiceContext.CreatePartial(contentService: Mock.Of<IContentService>());`
160160
{% endhint %}
161161

162-
## Testing an UmbracoApiController
162+
## Testing a Controller
163163

164164
See [Reference documentation on UmbracoApiControllers](../reference/routing/umbraco-api-controllers/README.md#locally-declared-controller).
165165

166-
{% hint style="warning" %}
167-
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
168-
{% endhint %}
169-
170166
```csharp
171-
public class ProductsController : UmbracoApiController
167+
[ApiController]
168+
[Route("/umbraco/api/products")]
169+
public class ProductsController : Controller
172170
{
173171
public IEnumerable<string> GetAllProducts()
174172
{
175173
return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
176174
}
177175

178-
[HttpGet]
176+
[HttpGet("getallproductsjson")]
179177
public JsonResult GetAllProductsJson()
180178
{
181179
return new JsonResult(this.GetAllProducts());
@@ -199,15 +197,15 @@ public class ProductsControllerTests
199197

200198
var result = this.controller.GetAllProducts();
201199

202-
Assert.AreEqual(expected, result);
200+
Assert.That(expected == result);
203201
}
204202

205203
[Test]
206204
public void WhenGetAllProductsJson_ThenReturnViewModelWithExpectedJson()
207205
{
208-
var json = serializer.Serialize(this.controller.GetAllProductsJson().Value);
206+
var json = JsonSerializer.Serialize(this.controller.GetAllProductsJson().Value);
209207

210-
Assert.AreEqual("[\"Table\",\"Chair\",\"Desk\",\"Computer\",\"Beer fridge\"]", json);
208+
Assert.That("[\"Table\",\"Chair\",\"Desk\",\"Computer\",\"Beer fridge\"]" == json);
211209
}
212210
}
213211
```

0 commit comments

Comments
 (0)