Skip to content

Commit

Permalink
Merge pull request #66 from ronaldkroon/Add_WhenLater
Browse files Browse the repository at this point in the history
Add WhenLater that defers the execution without extra parameter
  • Loading branch information
Erwinvandervalk authored Nov 27, 2017
2 parents a3b76cf + d989197 commit ff88e00
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Source/Core/Chill.Shared/GivenSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public Func<TResult> WhenAction
}
}

/// <summary>
/// Records the action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Func<TResult> whenFunc)
{
When(whenFunc, deferredExecution: true);
}

/// <summary>
/// Records the action that will trigger the actual test
/// </summary>
Expand All @@ -59,6 +67,15 @@ protected void When(Func<TResult> whenFunc, bool? deferredExecution = null)
EnsureTestTriggered(false);
}
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Func<Task<TResult>> whenFunc)
{
When(whenFunc, deferredExecution: true);
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test
/// </summary>
Expand Down Expand Up @@ -117,6 +134,14 @@ public Action WhenAction
}
}

/// <summary>
/// Records the action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
public void WhenLater(Action whenAction)
{
When(whenAction, deferredExecution: true);
}

/// <summary>
/// Records the action that will trigger the actual test
/// </summary>
Expand All @@ -138,6 +163,14 @@ public void When(Action whenAction, bool? deferredExecution = null)

}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
public void WhenLater(Func<Task> whenActionAsync)
{
When(whenActionAsync, deferredExecution: true);
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test
/// </summary>
Expand Down
60 changes: 60 additions & 0 deletions Source/Core/Chill.Tests.Shared/AsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
}
}

public class When_later_async_act_throws_in_a_test_with_subject : GivenSubject<object>
{
public When_later_async_act_throws_in_a_test_with_subject()
{
WhenLater(
async () =>
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
throw new ApplicationException();
});
}

[Fact]
public void Then_the_exception_should_be_observed()
{
WhenAction.ShouldThrow<ApplicationException>();
}

[Fact]
public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
{
WhenAction.ShouldNotThrow<AggregateException>();
}
}

public class When_a_deferred_async_act_throws_in_a_test_with_subject_and_result : GivenSubject<object, object>
{
public When_a_deferred_async_act_throws_in_a_test_with_subject_and_result()
Expand Down Expand Up @@ -160,6 +189,37 @@ public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
}
}

public class When_later_async_act_throws_in_a_test_with_subject_and_result : GivenSubject<object, object>
{
public When_later_async_act_throws_in_a_test_with_subject_and_result()
{
WhenLater((Func<Task<object>>)(
async () =>
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
throw new ApplicationException();
}));
}

[Fact]
public void Then_the_exception_should_be_observed()
{
Action action = () => WhenAction();
action.ShouldThrow<ApplicationException>();
}

[Fact]
public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
{
Action action = () => WhenAction();
action.ShouldNotThrow<AggregateException>();
}
}

public class When_an_async_arrange_is_used : GivenWhenThen
{
private BlockingCollection<int> results = new BlockingCollection<int>();
Expand Down
21 changes: 21 additions & 0 deletions Source/Core/Chill.Tests.Shared/CoreScenarios/GivenSubjectSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public void When_deferred_and_calling_when_then_whenaction_is_not_called_automat
message.Should().Be("givenwhen");
}

[Fact]
public void When_calling_whenlater_then_whenaction_is_not_called_automatically()
{
string message = "";
Given(() => message += "given");
WhenLater(() => message += "when");
message.Should().Be("given");
WhenAction();
message.Should().Be("givenwhen");
}

[Fact]
public void When_calling_async_when_method_the_func_is_awaited()
{
Expand Down Expand Up @@ -134,6 +145,16 @@ public void When_calling_when_directly_then_whenaction_is_not_called_on_result()
message.Should().Be("givenwhen");
Result.Should().Be("givenwhen");
}

[Fact]
public void When_calling_when_later_then_whenaction_is_called_on_result()
{
string message = "";
Given(() => message += "given");
WhenLater(() => message += "when");
message.Should().Be("given");
Result.Should().Be("givenwhen");
}
}

public class When_a_subject_is_build_using_a_custom_factory : GivenSubject<Disposable>
Expand Down
33 changes: 31 additions & 2 deletions Source/Examples/Chill.Examples.Tests/For_CustomerController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Chill;
using Chill.Autofac;
using System;
using Chill.Examples.Tests.TestSubjects;
using FluentAssertions;
using NSubstitute;
Expand Down Expand Up @@ -103,5 +102,35 @@ public void Then_model_is_the_existing_custmoer()
}
}

public class When_trying_to_retrieve_non_existing_customer : GivenSubject<CustomerController>
{
public When_trying_to_retrieve_non_existing_customer()
{
const int customerId = 12;

// Explicit phases for setup
Given(() =>
{
// Automatic creating of mock objects. Here using NSubstitute as a friendly mocking framework
The<ICustomerStore>().GetCustomer(customerId)
.Returns(args =>
{
throw new ArgumentException($"No customer with id {args[0]}");
});

});

// WhenLater will defer the execution so you can check on exceptions using the WhenAction
WhenLater(() => Subject.Get(customerId));
}



[Fact]
public void Then_an_argument_exception_should_be_thrown()
{
WhenAction.ShouldThrow<ArgumentException>().WithMessage("No customer with id 12");
}
}
}
}

0 comments on commit ff88e00

Please sign in to comment.