Skip to content

Commit

Permalink
Updated AspNet sample tests, switched from Machine.Specifications to …
Browse files Browse the repository at this point in the history
…Xunit/FluentAssertions
  • Loading branch information
mhelleborg committed Feb 1, 2024
1 parent 4cef580 commit f528287
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Samples/ASP.NET.Specs/ASP.NET.Specs.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../../specs.props" />
<Import Project="../../tests.props" />

<PropertyGroup>
<AssemblyName>Dolittle.SDK.ASP.NET.Specs</AssemblyName>
Expand Down
11 changes: 11 additions & 0 deletions Samples/ASP.NET.Specs/Kitchen/for_Kitchen/given/a_kitchen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Dolittle.SDK.Testing.Aggregates;

namespace Specs.Kitchen.for_Kitchen.given;

public abstract class a_kitchen(): AggregateRootTests<global::Kitchen.Kitchen>(kitchen_name)
{
const string kitchen_name = "Dolittle Tacos";
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using Kitchen;
using Machine.Specifications;
using Xunit;

namespace Specs.Kitchen.for_Kitchen.when_preparing_dish.in_one_operation;

class and_there_are_enough_ingredients : given.a_kitchen_with_no_prepared_dishes
public class and_there_are_enough_ingredients : given.a_kitchen
{
static string chef, dish;
Establish context = () =>
string chef, dish;

public and_there_are_enough_ingredients()
{
chef = "Sindre";
dish = "Tacos";
};

Because of = async () => await OnKitchen().Perform(_ =>
{
_.PrepareDish(chef, dish);
_.PrepareDish(chef, dish);
});

It should_after_last_operation_only_prepare_one_dish = () => KitchenAfterLastOperation().ShouldHaveEvent<DishPrepared>()
.CountOf(2)
.WhereAll(
_ => _.Chef.ShouldEqual(chef),
_ => _.Dish.ShouldEqual(dish));

It should_only_prepare_one_dish = () => KitchenWithAllEvents().ShouldHaveEvent<DishPrepared>()
.CountOf(2)
.AtEnd().Where(
_ => _.Chef.ShouldEqual(chef),
_ => _.Dish.ShouldEqual(dish));

It should_have_done_nothing_else = () => KitchenWithAllEvents().ShouldHaveNumberOfEvents(2);
WithAggregateInState(kitchen =>
{
kitchen.PrepareDish(chef, dish);
});
}

[Fact]
public void should_prepare_dish()
{
WhenPerforming(kitchen =>
{
kitchen.PrepareDish(chef, dish);
});

AssertThat
.ShouldHaveEvent<DishPrepared>()
.CountOf(1)
.WhereAll(
evt => evt.Chef.Should().Be(chef),
evt => evt.Dish.Should().Be(dish));
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using Dolittle.SDK.Aggregates;
using Machine.Specifications;
using FluentAssertions;
using Kitchen;
using Xunit;

namespace Specs.Kitchen.for_Kitchen.when_preparing_dish.in_one_operation;

class and_there_are_not_enough_ingredients : given.a_kitchen_with_no_prepared_dishes
public class and_there_are_not_enough_ingredients : given.a_kitchen
{
static Exception failure;

Because of = async () => failure = await Catch.ExceptionAsync(() => OnKitchen().Perform(customer =>
public and_there_are_not_enough_ingredients()
{
var num_ingredients = 2;
foreach (var _ in Enumerable.Range(0, num_ingredients + 1))
WithAggregateInState(kitchen =>
{
customer.PrepareDish("chef", "dish");
}
}));

It should_after_last_operation_not_have_done_anything = () => KitchenAfterLastOperation().ShouldHaveNoEvents();

It should_only_not_have_done_anything_at_all = () => KitchenWithAllEvents().ShouldHaveNoEvents();

It should_fail_because_operation_failed = () => failure.ShouldBeOfExactType<AggregateRootOperationFailed>();

kitchen.PrepareDish("chef", "dish");
kitchen.PrepareDish("chef", "dish");
});
}

[Fact]
public void should_fail_if_preparing_dish() =>
WhenPerforming(kitchen => kitchen.Invoking(
it => it.PrepareDish("chef", "dish")
)
.Should().Throw<OutOfIngredients>());
}
17 changes: 8 additions & 9 deletions Samples/ASP.NET.Specs/for_DishServer/given/the_handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
using Dolittle.SDK.Events;
using Dolittle.SDK.Testing.Aggregates;
using Kitchen;
using Machine.Specifications;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;

namespace Specs.for_DishServer.given;

class the_handler
public class the_handler
{
protected static readonly EventSourceId customer_name = DishServer.CustomerName;
protected static DishServer handler;
protected static AggregateOfMock<Customer> customers;
protected static EventSourceId kitchen_name;
protected static DishPrepared @event;
protected readonly EventSourceId customer_name = DishServer.CustomerName;
protected DishServer handler;
protected AggregateOfMock<Customer> customers;
protected EventSourceId kitchen_name;
protected DishPrepared @event;

Establish context = () =>
protected the_handler()
{
customers = AggregateOfMock<Customer>.Create(_ => _.AddLogging());
handler = new DishServer(customers, NullLogger<DishServer>.Instance);
kitchen_name = "Dolittle Tacos";
@event = new DishPrepared("some dish", "some chef");
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Customers;
using FluentAssertions;
using Kitchen;
using Machine.Specifications;
using Xunit;

namespace Specs.for_DishServer;

class when_a_dish_has_been_prepared : given.the_handler
public class when_a_dish_has_been_prepared : given.the_handler
{
Because of = async () => await handler.Handle(@event, Specs.given.an_event_context_for<DishPrepared>(kitchen_name));

It should_get_the_correct_customer = () => customers.TryGetAggregate(customer_name, out _).ShouldBeTrue();
public when_a_dish_has_been_prepared()
{
handler.Handle(@event, Specs.given.an_event_context_for<DishPrepared>(kitchen_name)).GetAwaiter().GetResult();
}

It the_customer_should_have_eaten_the_dish_once = () => customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
.CountOf(1)
.First().Where(_ => _.Dish.ShouldEqual(@event.Dish));

It the_customer_should_have_only_eaten_one_dish = () => customers.AssertThat(customer_name).ShouldHaveNumberOfEvents(1);
[Fact]
public void should_get_the_correct_customer()
{
customers.TryGetAggregate(customer_name, out _).Should().BeTrue();
}

[Fact]
public void should_have_eaten_the_dish_once()
{
customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
.CountOf(1)
.First().Where(_ => _.Dish.Should().Be(@event.Dish));
}

[Fact]
public void the_customer_should_have_only_eaten_one_dish()
{
customers.AssertThat(customer_name).ShouldHaveNumberOfEvents(1);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed under the M[Fact] public void license. See LICENSE file in the project root for full license information.

using Customers;
using FluentAssertions;
using Kitchen;
using Machine.Specifications;
using Xunit;

namespace Specs.for_DishServer;

class when_two_dishes_have_been_prepared : given.the_handler
public class when_two_dishes_have_been_prepared : given.the_handler
{
static DishPrepared first_dish, second_dish;
Establish context = () =>
DishPrepared first_dish, second_dish;

public when_two_dishes_have_been_prepared()
{
first_dish = @event with
{
Expand All @@ -25,26 +26,34 @@ class when_two_dishes_have_been_prepared : given.the_handler
};

handler.Handle(first_dish, Specs.given.an_event_context_for<DishPrepared>(kitchen_name)).GetAwaiter().GetResult();
};

Because of = async () => await handler.Handle(second_dish, Specs.given.an_event_context_for<DishPrepared>(kitchen_name));

It should_get_the_correct_customer = () => customers.TryGetAggregate(customer_name, out _).ShouldBeTrue();

It the_customer_should_have_only_eaten_second_dish_after_last_operation = () => customers.AfterLastOperationOn(customer_name).ShouldHaveEvent<DishEaten>()
.CountOf(1)
.First().Where(_ => _.Dish.ShouldEqual(second_dish.Dish));
handler.Handle(second_dish, Specs.given.an_event_context_for<DishPrepared>(kitchen_name)).GetAwaiter().GetResult();
}


[Fact]
public void should_get_the_correct_customer() => customers.TryGetAggregate(customer_name, out _).Should().BeTrue();

It the_customer_should_have_only_eaten_one_dish_after_last_operation = () => customers.AfterLastOperationOn(customer_name).ShouldHaveNumberOfEvents(1);
[Fact]
public void the_customer_should_have_only_eaten_second_dish_after_last_operation() => customers
.AfterLastOperationOn(customer_name)
.ShouldHaveSingleEvent<DishEaten>()
.Where(_ => _.Dish.Should().Be(second_dish.Dish));

It the_customer_should_have_eaten_first_dish_first = () => customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
[Fact]
public void the_customer_should_have_only_eaten_one_dish_after_last_operation() =>
customers.AfterLastOperationOn(customer_name).ShouldHaveNumberOfEvents(1);

[Fact]
public void the_customer_should_have_eaten_first_dish_first() => customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
.AtBeginning()
.Where(_ => _.Dish.ShouldEqual(first_dish.Dish));

It the_customer_should_have_eaten_second_dish_last = () => customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
.Where(_ => _.Dish.Should().Be(first_dish.Dish));

[Fact]
public void the_customer_should_have_eaten_second_dish_last() => customers.AssertThat(customer_name).ShouldHaveEvent<DishEaten>()
.AtEnd()
.Where(_ => _.Dish.ShouldEqual(second_dish.Dish));
.Where(_ => _.Dish.Should().Be(second_dish.Dish));

It the_customer_should_have_only_eaten_two_dishes = () => customers.AssertThat(customer_name).ShouldHaveNumberOfEvents(2);

[Fact]
public void the_customer_should_have_only_eaten_two_dishes() => customers.AssertThat(customer_name).ShouldHaveNumberOfEvents(2);
}
Loading

0 comments on commit f528287

Please sign in to comment.