コード例 #1
0
        public async Task When_task_completes_on_UI_thread_slow_async_it_should_fail()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <bool>();

            // Act
            Func <Task> action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds());

            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>();
        }
コード例 #2
0
        public async Task When_TCS_did_not_complete_in_time_and_it_is_not_expected_it_should_succeed()
        {
            // Arrange
            var subject = new TaskCompletionSource <bool>();
            var timer   = new FakeClock();

            // Act
            Func <Task> action = () => subject.Should(timer).NotCompleteWithinAsync(1.Seconds());

            timer.Complete();

            // Assert
            await action.Should().NotThrowAsync();
        }
コード例 #3
0
        public async Task When_TCS_did_not_complete_in_time_it_should_fail()
        {
            // Arrange
            var subject = new TaskCompletionSource <bool>();
            var timer   = new FakeClock();

            // Act
            Func <Task> action = () => subject.Should(timer).CompleteWithinAsync(1.Seconds(), "test {0}", "testArg");

            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected subject to complete within 1s because test testArg.");
        }
コード例 #4
0
        public async Task When_TCS_completes_in_time_and_async_result_is_expected_it_should_succeed()
        {
            // Arrange
            var subject = new TaskCompletionSource <int>();
            var timer   = new FakeClock();

            // Act
            Func <Task> action = () => subject.Should(timer).CompleteWithinAsync(1.Seconds()).WithResult(42);

            subject.SetResult(42);
            timer.Complete();

            // Assert
            await action.Should().NotThrowAsync();
        }
コード例 #5
0
        public async Task When_TCS_completes_in_time_and_it_is_not_expected_it_should_fail()
        {
            // Arrange
            var subject = new TaskCompletionSource <bool>();
            var timer   = new FakeClock();

            // Act
            Func <Task> action = () => subject.Should(timer).NotCompleteWithinAsync(1.Seconds(), "test {0}", "testArg");

            subject.SetResult(true);
            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>().WithMessage("*to not complete within*because test testArg*");
        }
コード例 #6
0
        public async Task When_task_completes_fast_async_it_should_succeed()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <bool>();

            // Act
            Func <Task> action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds());

            taskFactory.SetResult(true);
            timer.Complete();

            // Assert
            await action.Should().NotThrowAsync();
        }
コード例 #7
0
        public async Task When_TCS_completes_in_time_and_async_result_is_not_expected_it_should_fail()
        {
            // Arrange
            var testSubject = new TaskCompletionSource <int>();
            var timer       = new FakeClock();

            // Act
            Func <Task> action = () => testSubject.Should(timer).CompleteWithinAsync(1.Seconds()).WithResult(42);

            testSubject.SetResult(99);
            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected testSubject to be 42, but found 99.");
        }
コード例 #8
0
        public async Task When_should_is_passed_argument_context_should_still_be_found()
        {
            // Arrange
            var bob   = new TaskCompletionSource <bool>();
            var timer = new FakeClock();

            // Act
            Func <Task> action = () => bob.Should(timer).NotCompleteWithinAsync(1.Seconds(), "test {0}", "testArg");

            bob.SetResult(true);
            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected bob to not complete within 1s because test testArg.");
        }
コード例 #9
0
        public async Task When_task_completes_slow_async_it_should_fail()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <int>();

            // Act
            Func <Task> action = () =>
            {
                Func <Task <int> > func = () => taskFactory.Task;

                return(func.Should(timer).CompleteWithinAsync(100.Milliseconds()));
            };

            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>();
        }
コード例 #10
0
        public async Task When_task_completes_async_and_async_result_is_not_expected_it_should_throw()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <int>();

            // Act
            Func <Task> action = async() =>
            {
                Func <Task <int> > funcSubject = () => taskFactory.Task;

                await funcSubject.Should(timer).CompleteWithinAsync(100.Milliseconds()).WithResult(42);
            };

            taskFactory.SetResult(99);
            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>().WithMessage("Expected funcSubject to be 42, but found 99.");
        }
コード例 #11
0
        public async Task When_task_completes_fast_async_it_should_succeed()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <int>();

            // Act
            Func <Task> action = async() =>
            {
                Func <Task <int> > func = () => taskFactory.Task;

                (await func.Should(timer).CompleteWithinAsync(100.Milliseconds()))
                .Which.Should().Be(42);
            };

            taskFactory.SetResult(42);
            timer.Complete();

            // Assert
            await action.Should().NotThrowAsync();
        }
コード例 #12
0
        public async Task When_task_does_not_throw_async_on_UI_thread_it_should_succeed()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <int>();

            // Act
            Func <Task> action = async() =>
            {
                Func <Task <int> > func = () => taskFactory.Task;

                (await func.Should(timer).NotThrowAsync())
                .Which.Should().Be(42);
            };

            taskFactory.SetResult(42);
            timer.Complete();

            // Assert
            await action.Should().NotThrowAsync();
        }
コード例 #13
0
        public async Task Sync_work_in_async_method_is_taken_into_account()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <bool>();

            // Act
            Func <Task> action = () => taskFactory
                                 .Awaiting(t =>
            {
                timer.Delay(101.Milliseconds());
                return((Task)t.Task);
            })
                                 .Should(timer)
                                 .CompleteWithinAsync(100.Milliseconds());

            taskFactory.SetResult(true);
            timer.Complete();

            // Assert
            await action.Should().ThrowAsync <XunitException>();
        }
コード例 #14
0
        public async Task When_task_completes_async_and_result_is_not_expected_it_should_throw()
        {
            // Arrange
            var timer       = new FakeClock();
            var taskFactory = new TaskCompletionSource <int>();

            // Act
            Func <Task> action = async() =>
            {
                Func <Task <int> > funcSubject = () => taskFactory.Task;

                (await funcSubject.Should(timer).CompleteWithinAsync(100.Milliseconds()))
                .Which.Should().Be(42);
            };

            taskFactory.SetResult(99);
            timer.Complete();

            // Assert
            // TODO message currently shows "Expected (await funcSubject to be...", but should be "Expected funcSubject to be...",
            // maybe with or without await.
            await action.Should().ThrowAsync <XunitException>().WithMessage("*to be 42, but found 99.");
        }