Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Source/Bifrost.Specs/Bifrost.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Commands\for_CommandHandlerInvoker\Subjects.cs" />
<Compile Include="Commands\for_CommandHandlerInvoker\when_handling_with_automatically_discovered_command_handlers.cs" />
<Compile Include="Commands\for_CommandHandlerInvoker\when_handling_with_manually_registered_command_handlers.cs" />
<Compile Include="Commands\for_CommandHandlerInvoker\when_receiving_asynchronous_initialization.cs" />
<Compile Include="Commands\for_CommandHandlerInvoker\when_handling_with_no_command_handlers.cs" />
<Compile Include="Commands\for_CommandHandlerManager\given\a_command_handler_manager.cs" />
<Compile Include="Commands\for_CommandHandlerManager\when_handling_a_command_without_a_command_handler.cs" />
Expand Down Expand Up @@ -326,6 +327,12 @@
<Compile Include="Execution\for_InstancesOf\OneImplementation.cs" />
<Compile Include="Execution\for_InstancesOf\SecondImplementation.cs" />
<Compile Include="Execution\for_InstancesOf\when_having_multiple_implementations.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\given\an_ordered_instances_of.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\when_instances_are_ordered.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\when_instances_are_ordered_incorrectly.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\when_instances_have_circular_dependencies.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\when_instances_have_dependencies.cs" />
<Compile Include="Execution\for_OrderedInstancesOf\when_there_are_no_instances.cs" />
<Compile Include="Execution\for_TypeDiscoverer\when_finding_type_by_name_that_does_not_exist.cs" />
<Compile Include="Execution\for_TypeDiscoverer\when_finding_type_by_name_that_exists.cs" />
<Compile Include="Execution\for_TypeFinder\given\a_type_finder.cs" />
Expand Down Expand Up @@ -374,6 +381,10 @@
<Compile Include="Extensions\for_StringExtensions\when_converting_a_string_with_pascal_casing_to_camel_casing.cs" />
<Compile Include="Extensions\for_TypeExtensions\ClassImplementingGenericInterface.cs" />
<Compile Include="Extensions\for_TypeExtensions\ConceptType.cs" />
<Compile Include="Extensions\for_TypeExtensions\Custom1Attribute.cs" />
<Compile Include="Extensions\for_TypeExtensions\Custom2Attribute.cs" />
<Compile Include="Extensions\for_TypeExtensions\when_getting_attributes.cs" />
<Compile Include="Extensions\for_TypeExtensions\when_checking_if_types_have_attribute.cs" />
<Compile Include="Extensions\for_TypeExtensions\IInterfaceWithGenericArguments.cs" />
<Compile Include="Extensions\for_TypeExtensions\SomeObject.cs" />
<Compile Include="Extensions\for_TypeExtensions\when_asking_type_if_implements_generic_interface_without_specifying_generic_arguments.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Bifrost.Commands;
using Bifrost.Exceptions;
using Bifrost.Security;
using Bifrost.Validation;
using Machine.Specifications;
using Moq;

Expand All @@ -13,15 +13,17 @@ public abstract class a_command_coordinator : Globalization.given.a_localizer_mo
protected static Mock<ICommandContextManager> command_context_manager_mock;
protected static Mock<ICommandSecurityManager> command_security_manager_mock;
protected static Mock<ICommandValidators> command_validators_mock;
protected static Mock<ICommand> command_mock;
protected static Mock<ICommandContext> command_context_mock;
protected static Mock<IExceptionPublisher> exception_publisher_mock;
protected static ICommand command;

Establish context = () =>
{
command_mock = new Mock<ICommand>();
command = Mock.Of<ICommand>();
command_handler_manager_mock = new Mock<ICommandHandlerManager>();
command_context_manager_mock = new Mock<ICommandContextManager>();
command_validators_mock = new Mock<ICommandValidators>();
exception_publisher_mock = new Mock<IExceptionPublisher>();

command_context_mock = new Mock<ICommandContext>();
command_context_manager_mock.Setup(c => c.EstablishForCommand(Moq.It.IsAny<ICommand>())).
Expand All @@ -36,7 +38,8 @@ public abstract class a_command_coordinator : Globalization.given.a_localizer_mo
command_context_manager_mock.Object,
command_security_manager_mock.Object,
command_validators_mock.Object,
localizer_mock.Object
localizer_mock.Object,
exception_publisher_mock.Object
);
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Bifrost.Commands;
using Bifrost.Security;
using Machine.Specifications;
using Bifrost.Testing.Fakes.Commands;
using Moq;
using It = Machine.Specifications.It;

Expand All @@ -10,7 +9,6 @@ namespace Bifrost.Specs.Commands.for_CommandCoordinator
[Subject(typeof(CommandCoordinator))]
public class when_handling_a_command_that_fails_security : given.a_command_coordinator
{
static ICommand command;
static CommandResult result;
static Mock<AuthorizationResult> authorization_result;

Expand All @@ -19,8 +17,7 @@ public class when_handling_a_command_that_fails_security : given.a_command_coord
authorization_result = new Mock<AuthorizationResult>();
authorization_result.Setup(r => r.IsAuthorized).Returns(false);
authorization_result.Setup(r => r.BuildFailedAuthorizationMessages()).Returns(new[] { "Something went wrong" });
command = new SimpleCommand();
command_security_manager_mock.Setup(c => c.Authorize(Moq.It.IsAny<ICommand>())).Returns(authorization_result.Object);
command_security_manager_mock.Setup(c => c.Authorize(command)).Returns(authorization_result.Object);
};

Because of = () => result = coordinator.Handle(command);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
using Bifrost.Commands;
using Machine.Specifications;
using Bifrost.Validation;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Commands.for_CommandCoordinator
{
[Subject(typeof(CommandCoordinator))]
public class when_handling_an_invalid_command : given.a_command_coordinator
{
static CommandResult Result;
static CommandResult result;
static CommandValidationResult validation_errors;

Establish context = () =>
{
validation_errors = new CommandValidationResult
{
ValidationResults = new ValidationResult[]
{
new ValidationResult("First validation failure"),
new ValidationResult("Second validation failure")
}
};
{
validation_errors = new CommandValidationResult
{
ValidationResults = new[]
{
new ValidationResult("First validation failure"),
new ValidationResult("Second validation failure")
}
};

command_validators_mock.Setup(cvs => cvs.Validate(command_mock.Object)).Returns(
validation_errors);
};
command_validators_mock
.Setup(cvs => cvs.Validate(command))
.Returns(validation_errors);
};

Because of = () => Result = coordinator.Handle(command_mock.Object);

Because of = () => result = coordinator.Handle(command);

It should_have_validated_the_command = () => command_validators_mock.VerifyAll();
It should_have_a_result = () => Result.ShouldNotBeNull();
It should_have_success_set_to_false = () => Result.Success.ShouldBeFalse();
It should_have_a_record_of_each_validation_failure = () => Result.ValidationResults.ShouldContainOnly(validation_errors.ValidationResults);
It should_not_handle_the_command = () => command_handler_manager_mock.Verify(chm => chm.Handle(command_mock.Object), Moq.Times.Never());
It should_rollback_the_command_context = () => command_context_mock.Verify(c => c.Rollback(), Moq.Times.Once());
It should_have_a_result = () => result.ShouldNotBeNull();
It should_have_success_set_to_false = () => result.Success.ShouldBeFalse();
It should_have_a_record_of_each_validation_failure = () => result.ValidationResults.ShouldContainOnly(validation_errors.ValidationResults);
It should_not_handle_the_command = () => command_handler_manager_mock.Verify(chm => chm.Handle(command), Times.Never());
It should_rollback_the_command_context = () => command_context_mock.Verify(c => c.Rollback(), Times.Once());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Bifrost.Commands;
using Bifrost.Exceptions;
using Machine.Specifications;

namespace Bifrost.Specs.Commands.for_CommandCoordinator
Expand All @@ -11,14 +12,15 @@ public class when_handling_command_and_an_exception_occurs_during_authorization
static Exception exception;

Establish context = () =>
{
exception = new Exception();
command_security_manager_mock.Setup(cvs => cvs.Authorize(command_mock.Object)).Throws(exception);
};
{
exception = new Exception();
command_security_manager_mock.Setup(cvs => cvs.Authorize(command)).Throws(exception);
};

Because of = () => result = coordinator.Handle(command_mock.Object);
Because of = () => result = coordinator.Handle(command);

It should_have_exception_in_result = () => result.Exception.ShouldEqual(exception);
It should_have_success_set_to_false = () => result.Success.ShouldBeFalse();
It should_have_published_the_exception = () => exception_publisher_mock.Verify(m => m.Publish(exception));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using Bifrost.Commands;
using Machine.Specifications;
using It = Machine.Specifications.It;
using Bifrost.Exceptions;
using Bifrost.Validation;
using Machine.Specifications;

namespace Bifrost.Specs.Commands.for_CommandCoordinator
{
Expand All @@ -13,18 +13,19 @@ public class when_handling_command_and_an_exception_occurs_during_handling : giv
static Exception exception;

Establish context = () =>
{
exception = new Exception();
var validation_results = new CommandValidationResult { ValidationResults = new ValidationResult[0] };
command_validators_mock.Setup(cvs => cvs.Validate(command_mock.Object)).Returns(validation_results);
command_handler_manager_mock.Setup(c => c.Handle(Moq.It.IsAny<ICommand>())).Throws(exception);
};
{
exception = new Exception();
var validationResults = new CommandValidationResult {ValidationResults = new ValidationResult[0]};
command_validators_mock.Setup(cvs => cvs.Validate(command)).Returns(validationResults);
command_handler_manager_mock.Setup(c => c.Handle(Moq.It.IsAny<ICommand>())).Throws(exception);
};

Because of = () => result = coordinator.Handle(command_mock.Object);
Because of = () => result = coordinator.Handle(command);

It should_have_validated_the_command = () => command_validators_mock.VerifyAll();
It should_have_authenticated_the_command = () => command_security_manager_mock.VerifyAll();
It should_have_exception_in_result = () => result.Exception.ShouldEqual(exception);
It should_have_success_set_to_false = () => result.Success.ShouldBeFalse();
It should_have_published_the_exception = () => exception_publisher_mock.Verify(m => m.Publish(exception));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Bifrost.Commands;
using Bifrost.Exceptions;
using Machine.Specifications;

namespace Bifrost.Specs.Commands.for_CommandCoordinator
Expand All @@ -11,15 +12,16 @@ public class when_handling_command_and_an_exception_occurs_during_validation : g
static Exception exception;

Establish context = () =>
{
exception = new Exception();
command_validators_mock.Setup(cvs => cvs.Validate(command_mock.Object)).Throws(exception);
};
{
exception = new Exception();
command_validators_mock.Setup(cvs => cvs.Validate(command)).Throws(exception);
};

Because of = () => result = coordinator.Handle(command_mock.Object);
Because of = () => result = coordinator.Handle(command);

It should_have_authorized_the_command = () => command_security_manager_mock.VerifyAll();
It should_have_exception_in_result = () => result.Exception.ShouldEqual(exception);
It should_have_success_set_to_false = () => result.Success.ShouldBeFalse();
It should_have_published_the_exception = () => exception_publisher_mock.Verify(m => m.Publish(exception));
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
using Bifrost.Commands;
using Machine.Specifications;
using It = Machine.Specifications.It;
using Bifrost.Validation;

namespace Bifrost.Specs.Commands.for_CommandCoordinator
{
[Subject(typeof(CommandCoordinator))]
public class when_handling_command_with_success : given.a_command_coordinator
{
static CommandResult Result;
static CommandResult result;

Establish context = () =>
{
var validation_results = new CommandValidationResult();
command_validators_mock.Setup(cvs => cvs.Validate(command_mock.Object)).Returns(validation_results);
};
command_validators_mock.Setup(cvs => cvs.Validate(command)).Returns(new CommandValidationResult());

Because of = () =>
{
Result = coordinator.Handle(command_mock.Object);
};
Because of = () => result = coordinator.Handle(command);

It should_have_validated_the_command = () => command_validators_mock.VerifyAll();
It should_have_a_result = () => Result.ShouldNotBeNull();
It should_have_success_set_to_true = () => Result.Success.ShouldBeTrue();
It should_have_a_result = () => result.ShouldNotBeNull();
It should_have_success_set_to_true = () => result.Success.ShouldBeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using Bifrost.Commands;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Commands.for_CommandHandlerInvoker
{
[Subject(Subjects.handling_commands)]
public class when_receiving_asynchronous_initialization : given.a_command_handler_invoker_with_no_command_handlers
{
protected static bool result;

Because of = () =>
{
var command = new Command();
var thread = new Thread(() => invoker.TryHandle(command));

type_discoverer_mock
.Setup(t => t.FindMultiple<IHandleCommands>())
.Callback(
() =>
{
thread.Start();
Thread.Sleep(50);
})
.Returns(new Type[0]);
result = invoker.TryHandle(command);
thread.Join();
};

It should_initialize_only_once = () =>
type_discoverer_mock.Verify(m => m.FindMultiple<IHandleCommands>(), Times.Once);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;

namespace Bifrost.Specs.Execution.for_OrderedInstancesOf.given
{
public class an_ordered_instances_of
{
public interface IDummy { }

protected static Mock<ITypeDiscoverer> type_discoverer_mock;
protected static Mock<IContainer> container_mock;
protected static OrderedInstancesOf<IDummy> ordered_instances_of;

protected static IDummy[] result;

Establish context = () =>
{
type_discoverer_mock = new Mock<ITypeDiscoverer>();
container_mock = new Mock<IContainer>();
};

protected static void Register(params IDummy[] instances)
{
type_discoverer_mock
.Setup(m => m.FindMultiple<IDummy>())
.Returns(instances.Select(i => i.GetType()));
foreach (var instance in instances)
{
container_mock
.Setup(m => m.Get(instance.GetType()))
.Returns(instance);
}

ordered_instances_of = new OrderedInstancesOf<IDummy>(type_discoverer_mock.Object, container_mock.Object);
}
}
}
Loading