-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCommandTests.cs
55 lines (48 loc) · 1.8 KB
/
CommandTests.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
using System.Text;
using System.Text.Json;
using CommandQuery.AzureFunctions;
using CommandQuery.Sample.Contracts.Commands;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
namespace CommandQuery.Sample.AzureFunctions.Tests
{
public class CommandTests
{
[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
Program.ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
Subject = new Command(serviceProvider.GetRequiredService<ICommandFunction>());
var context = new Mock<FunctionContext>();
context.SetupProperty(c => c.InstanceServices, serviceProvider);
Context = context.Object;
}
[Test]
public async Task should_handle_command()
{
var result = await Subject.Run(GetRequest(new FooCommand { Value = "Foo" }), Context, "FooCommand");
result.As<IStatusCodeActionResult>().StatusCode.Should().Be(200);
}
[Test]
public async Task should_handle_errors()
{
var result = await Subject.Run(GetRequest(new FooCommand()), Context, "FooCommand");
result.ShouldBeError("Value cannot be null or empty");
}
HttpRequest GetRequest(object body)
{
var request = new Mock<HttpRequest>();
request.Setup(r => r.Body).Returns(new MemoryStream(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(body))));
return request.Object;
}
Command Subject = null!;
FunctionContext Context = null!;
}
}