public async Task when_handling_the_command() { CommandName = "FakeCommand"; FakeCommandProcessor.Setup(x => x.GetCommandType(CommandName)).Returns(typeof(FakeCommand)); async Task should_invoke_the_command_processor() { FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Returns(Task.CompletedTask); var result = await Subject.Handle(CommandName, Json); (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200); } async Task should_handle_CommandProcessorException() { FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandProcessorException("fail")); var result = await Subject.Handle(CommandName, Json); await result.ShouldBeErrorAsync("fail", 400); } async Task should_handle_CommandException() { FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandException("invalid")); var result = await Subject.Handle(CommandName, Json); await result.ShouldBeErrorAsync("invalid", 400); } async Task should_handle_Exception() { FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new Exception("fail")); var result = await Subject.Handle(CommandName, Json); await result.ShouldBeErrorAsync("fail", 500); } async Task should_log_errors() { var fakeTraceWriter = new Mock <ITraceWriter>(); var subject = new FakeCommandController(FakeCommandProcessor.Object, fakeTraceWriter.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new Exception("fail")); await subject.Handle(CommandName, Json); fakeTraceWriter.Verify(x => x.Trace(It.IsAny <HttpRequestMessage>(), "UnhandledCommandException", TraceLevel.Error, It.IsAny <Action <TraceRecord> >())); } }
public void SetUp() { FakeCommandProcessor = new Mock <ICommandProcessor>(); Subject = new FakeCommandController(FakeCommandProcessor.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; }