public void NoResultPassExceptionFactoryWithCallArg(IFoo fake, Task task) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And an async method of the fake configured to throw asynchronously" .x(() => A.CallTo(() => fake.BarAsync()).ThrowsAsync(call => new MyException())); "When that method is called" .x(() => { task = fake.BarAsync(); }); "Then it returns a failed task" .x(() => task.Status.Should().Be(TaskStatus.Faulted)); "And the task's exception is the configured exception" .x(() => task.Exception?.InnerException.Should().BeAnExceptionOfType<MyException>()); }
public static void DoesNothingOnTaskReturningMethod(IFoo fake, Task result) { "Given a fake of a class with a task-returning method" .x(() => fake = A.Fake <IFoo>()); "And the method is configured to return an failed task" .x(() => A.CallTo(() => fake.BarAsync()).Returns(Task.FromException(new Exception("oops")))); "When the method is configured to do nothing" .x(() => A.CallTo(() => fake.BarAsync()).DoesNothing()); "And the method is called" .x(() => result = fake.BarAsync()); "Then it returns a successfully completed task" .x(() => result.Status.Should().Be(TaskStatus.RanToCompletion)); }
public static void NoResultPassExceptionFactoryWithCallArg(IFoo fake, Task task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And an async method of the fake configured to throw asynchronously" .x(() => A.CallTo(() => fake.BarAsync()).ThrowsAsync(call => new MyException())); "When that method is called" .x(() => { task = fake.BarAsync(); }); "Then it returns a failed task" .x(() => task.Status.Should().Be(TaskStatus.Faulted)); "And the task's exception is the configured exception" .x(() => (task.Exception?.InnerException).Should().BeAnExceptionOfType <MyException>()); }
public void NoResultPassExceptionFactoryWithFourArgs(IFoo fake, Task task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And an async method of the fake configured to throw asynchronously" .x(() => A.CallTo(() => fake.BarAsync(0, "x", false, 0.0)) .ThrowsAsync((int a, string b, bool c, double d) => new MyException())); "When that method is called" .x(() => { task = fake.BarAsync(0, "x", false, 0.0); }); "Then it returns a failed task" .x(() => task.Status.Should().Be(TaskStatus.Faulted)); "And the task's exception is the configured exception" .x(() => task.Exception?.InnerException.Should().BeAnExceptionOfType <MyException>()); }
public void AsyncWithResultCanceledTokenWithConfiguredCallForNonCanceledToken( IFoo fake, CancellationToken cancellationToken, Task <int> task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a call configured on that fake for a non-canceled token" .x(() => A.CallTo(() => fake.BarAsync(A <CancellationToken> .That.IsNotCanceled())).Returns(42)); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When the configured async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a canceled task" .x(() => task.Status.Should().Be(TaskStatus.Canceled)); }
public void AsyncWithResultCanceledTokenWithConfiguredCallForAnyToken( IFoo fake, CancellationToken cancellationToken, Task <int> task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a call configured on that fake" .x(() => A.CallTo(() => fake.BarAsync(A <CancellationToken> ._)).Returns(42)); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When the configured async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a successfully completed task" .x(() => task.Status.Should().Be(TaskStatus.RanToCompletion)); "And the task's result is the configured value" .x(() => task.Result.Should().Be(42)); }
public void AsyncWithResultCanceledToken( IFoo fake, CancellationToken cancellationToken, Task <int> task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When an async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a canceled task" .x(() => task.Status.Should().Be(TaskStatus.Canceled)); }
public void AsyncWithResultNonCanceledToken( IFoo fake, CancellationToken cancellationToken, Task <int> task) { "Given a fake" .x(() => fake = A.Fake <IFoo>()); "And a cancellation token that is not canceled" .x(() => cancellationToken = new CancellationToken(false)); "When an async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a successfully completed task" .x(() => task.Status.Should().Be(TaskStatus.RanToCompletion)); "And the task's result should be the default value" .x(() => task.Result.Should().Be(0)); }
public void AsyncWithResultCanceledTokenWithConfiguredCallForNonCanceledToken( IFoo fake, CancellationToken cancellationToken, Task<int> task) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a call configured on that fake for a non-canceled token" .x(() => A.CallTo(() => fake.BarAsync(A<CancellationToken>.That.IsNotCanceled())).Returns(42)); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When the configured async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a canceled task" .x(() => task.Status.Should().Be(TaskStatus.Canceled)); }
public void AsyncWithResultCanceledTokenWithConfiguredCallForAnyToken( IFoo fake, CancellationToken cancellationToken, Task<int> task) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a call configured on that fake" .x(() => A.CallTo(() => fake.BarAsync(A<CancellationToken>._)).Returns(42)); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When the configured async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a successfully completed task" .x(() => task.Status.Should().Be(TaskStatus.RanToCompletion)); "And the task's result is the configured value" .x(() => task.Result.Should().Be(42)); }
public void AsyncWithResultCanceledToken( IFoo fake, CancellationToken cancellationToken, Task<int> task) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a cancellation token that is canceled" .x(() => cancellationToken = new CancellationToken(true)); "When an async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a canceled task" .x(() => task.Status.Should().Be(TaskStatus.Canceled)); }
public void AsyncWithResultNonCanceledToken( IFoo fake, CancellationToken cancellationToken, Task<int> task) { "Given a fake" .x(() => fake = A.Fake<IFoo>()); "And a cancellation token that is not canceled" .x(() => cancellationToken = new CancellationToken(false)); "When an async method is called with this cancellation token" .x(() => { task = fake.BarAsync(cancellationToken); }); "Then it returns a successfully completed task" .x(() => task.Status.Should().Be(TaskStatus.RanToCompletion)); "And the task's result should be the default value" .x(() => task.Result.Should().Be(0)); }