Пример #1
0
        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>());
        }
Пример #2
0
        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));
        }
Пример #3
0
        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>());
        }
Пример #4
0
        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>());
        }
Пример #5
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));
        }
Пример #6
0
        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));
        }
Пример #7
0
        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));
        }
Пример #8
0
        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));
        }
Пример #9
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));
        }
Пример #10
0
        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));
        }
Пример #11
0
        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));
        }
Пример #12
0
        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));
        }