Skip to content

Instantly share code, notes, and snippets.

@Flayed
Flayed / my.component.html
Created March 29, 2018 16:27
Angular Select
<select [(ngModel)]="myValue">
<option *ngFor="let s of stuff" [value]="s.value">{{s.key}}</option>
</select>
@Flayed
Flayed / ControllerTestBase.cs
Created March 23, 2018 12:03
Controller Test Base extension
protected T IsOK<T>(IActionResult result) =>
Is<OkObjectResult, T>(result, HttpStatusCode.OK);
protected void IsNoContent(IActionResult result) =>
Is<NoContentResult>(result, HttpStatusCode.NoContent);
protected void IsBadRequest(IActionResult result) =>
Is<BadRequestResult>(result, HttpStatusCode.BadRequest);
protected void IsNotFound(IActionResult result) =>
@Flayed
Flayed / ControllerTestBase.cs
Created March 23, 2018 12:00
Unit testing IActionResult
public class ControllerTestBase
{
protected void Is<T>(IActionResult result, HttpStatusCode statusCode) where T : StatusCodeResult
{
var r = result as T;
r.Should().NotBeNull();
r.StatusCode.Should().Be((int)statusCode);
}
protected TPayload Is<TResult, TPayload>(IActionResult result, HttpStatusCode statusCode) where TResult : ObjectResult
@Flayed
Flayed / IActionResult.cs
Created March 23, 2018 11:47
IActionResult from ASP.NET core
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// Defines a contract that represents the result of an action method.
/// </summary>
@Flayed
Flayed / ControllerExtensions.cs
Created March 22, 2018 17:49
ControllerBase InternalServerErrorResult
public static class ControllerExtensions
{
public static InternalServerErrorResult InternalServerError(this Controller controller)
{
return new InternalServerErrorResult();
}
}
@Flayed
Flayed / MyController.cs
Created March 22, 2018 16:34
MVC controller with IActionResult
public class MyController: Controller
{
public MyController(IComplexTypeService complexTypeService)
{
_complexTypeService = complexTypeService;
}
[HttpPost("{id}")]
public async Task<IActionResult> AddComplexType(string id, [FromBody]ComplexType cType)
{
@Flayed
Flayed / RawCap.cmd
Created March 14, 2018 12:03
RawCap
RawCap.exe -f 127.0.0.1 dumpfile.pcap
@Flayed
Flayed / MyServiceTests.cs
Last active March 11, 2018 18:05
MyServiceTests with Provide
public class MyServiceTests : TestBase
{
[Fact(DisplayName = "CalculateDistance should get the addresses from the address service")]
public async Task CalculateDistance_LooksUpAddressesInAddressService()
{
double expected = 15623.32944;
Mock.Mock<IAddressService>().SetupGet(x => x.GetAddress(It.IsAny<string>())).ReturnsAsync("123 Fake St, Duck, WV, 25063");
Provide<IDistanceCalculationService, DistanceCalculationService>();
var myService = Mock.Create<MyService>();
@Flayed
Flayed / MyServiceTests.cs
Last active March 11, 2018 18:16
MyService tests using the TestBase
public class MyServiceTests : TestBase
{
[Fact(DisplayName = "CalculateDistance should get the addresses from the address service")]
public async Task CalculateDistance_LooksUpAddressesInAddressService()
{
double expected = 15000;
Mock.Mock<IAddressService>().SetupGet(x => x.Address).Returns("123 Fake St, Duck, WV, 25063");
Mock.Mock<IDistanceCalculationService>().Setup(x => x.CalculateDistance(It.IsAny<string>())).ReturnsAsync(expected);
var myService = Mock.Create<MyService>();
@Flayed
Flayed / TestBase.cs
Created March 11, 2018 17:52
TestBase
public class TestBase : IDisposable
{
public readonly AutoMock Mock;
public TestBase()
{
Mock = AutoMock.GetLoose();
}
public TImplementation Provide<TInterface, TImplementation>()
where TInterface : class