Ejemplo n.º 1
0
        public void When_subject_throws_the_expected_exact_exception_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act / Assert
            //-----------------------------------------------------------------------------------------------------------
            asyncObject
            .Awaiting(x => x.ThrowAsync <ArgumentNullException>())
            .Should().ThrowExactly <ArgumentNullException>();
        }
        public void When_subject_throws_subclass_of_expected_exact_exception_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Action action = () => asyncObject
                            .Awaiting(x => x.ThrowAsync <ArgumentNullException>())
                            .Should().ThrowExactly <ArgumentException>("because {0} should do that", "IFoo.Do");

            // Assert
            action.Should().Throw <XunitException>()
            .WithMessage("Expected type to be System.ArgumentException because IFoo.Do should do that, but found System.ArgumentNullException.");
        }
        public async Task When_async_method_throws_exception_through_ValueTask_and_no_exception_was_expected_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Func <Task> action = () => asyncObject
                                 .Awaiting(x => x.ThrowAsyncValueTask <ArgumentException>())
                                 .Should().NotThrowAsync();

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Did not expect any exception, but found System.ArgumentException*");
        }
        public async Task When_ValueTask_async_method_of_T_throws_exception_expected_not_to_be_thrown_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Func <Task> action = () => asyncObject
                                 .Awaiting(x => x.ThrowValueTaskIntAsync <ArgumentException>(true))
                                 .Should().NotThrowAsync <ArgumentException>();

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Did not expect System.ArgumentException, but found System.ArgumentException*");
        }
        public async Task When_async_method_throws_unexpected_exception_through_ValueTask_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Func <Task> action = () => asyncObject
                                 .Awaiting(x => x.ThrowAsyncValueTask <ArgumentException>())
                                 .Should().ThrowAsync <InvalidOperationException>("because {0} should do that", "IFoo.Do");

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected a <System.InvalidOperationException> to be thrown because IFoo.Do should do that, but found <System.ArgumentException>*");
        }
        public async Task When_async_method_does_not_throw_expected_exception_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Func <Task> action = () => asyncObject
                                 .Awaiting(x => x.SucceedAsync())
                                 .Should().ThrowAsync <InvalidOperationException>("because {0} should do that", "IFoo.Do");

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected a <System.InvalidOperationException> to be thrown because IFoo.Do should do that, but no exception was thrown.");
        }
        public async Task When_subject_throws_aggregate_exception_and_not_expected_exact_exception_through_ValueTask_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Func <Task> action = () => asyncObject
                                 .Awaiting(x => x.ThrowAggregateExceptionAsyncValueTask <ArgumentException>())
                                 .Should().ThrowExactlyAsync <ArgumentException>("because {0} should do that", "IFoo.Do");

            // Assert
            await action.Should().ThrowAsync <XunitException>()
            .WithMessage("Expected type to be System.ArgumentException because IFoo.Do should do that, but found System.AggregateException.");
        }
        public void When_async_method_throws_exception_expected_not_to_be_thrown_it_should_fail()
        {
            // Arrange
            var asyncObject = new AsyncClass();

            // Act
            Action action = () => asyncObject
                            .Awaiting(x => x.ThrowAsync <ArgumentException>())
                            .Should().NotThrow <ArgumentException>();

            // Assert
            action.Should().Throw <XunitException>()
            .WithMessage("Did not expect System.ArgumentException, but found*");
        }
        public async Task When_poll_interval_is_zero_for_async_func_executed_with_wait_it_should_not_throw()
        {
            // Arrange
            var waitTime     = 10.Milliseconds();
            var pollInterval = 0.Milliseconds();

            var         clock       = new FakeClock();
            var         asyncObject = new AsyncClass();
            Func <Task> someFunc    = () => asyncObject.SucceedAsync();

            // Act
            Func <Task> act = () => someFunc.Should(clock).NotThrowAfterAsync(waitTime, pollInterval);

            // Assert
            await act.Should().NotThrowAsync();
        }
Ejemplo n.º 10
0
        public async Task When_poll_interval_is_negative_and_expecting_to_not_throw_async_it_should_throw()
        {
            // Arrange
            var waitTime     = 10.Milliseconds();
            var pollInterval = -1.Milliseconds();

            var asyncObject             = new AsyncClass();
            Func <Task <int> > someFunc = () => asyncObject.ReturnTaskInt();

            // Act
            Func <Task> act = () => someFunc.Should().NotThrowAfterAsync(waitTime, pollInterval);

            // Assert
            await act.Should().ThrowAsync <ArgumentOutOfRangeException>()
            .WithMessage("* value of pollInterval must be non-negative*");
        }
        public void When_poll_interval_is_negative_for_async_func_executed_with_wait_it_should_throw()
        {
            // Arrange
            var waitTime     = 10.Milliseconds();
            var pollInterval = -1.Milliseconds();

            var         asyncObject = new AsyncClass();
            Func <Task> someFunc    = () => asyncObject.SucceedAsync();

            // Act
            Action act = () => someFunc.Should().NotThrowAfter(waitTime, pollInterval);

            // Assert
            act.Should().Throw <ArgumentOutOfRangeException>()
            .WithMessage("* value of pollInterval must be non-negative*");
        }
        public async Task When_wait_time_is_negative_for_async_func_it_should_throw()
        {
            // Arrange
            var waitTime     = -1.Milliseconds();
            var pollInterval = 10.Milliseconds();

            var         asyncObject = new AsyncClass();
            Func <Task> someFunc    = () => asyncObject.SucceedAsync();

            // Act
            Func <Task> act = () => someFunc.Should().NotThrowAfterAsync(waitTime, pollInterval);

            // Assert
            await act.Should().ThrowAsync <ArgumentOutOfRangeException>()
            .WithMessage("* value of waitTime must be non-negative*");
        }
        public async Task When_async_method_throws_exception_and_expected_not_to_throw_async_another_one_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Func <Task> action = async() => await asyncObject.ThrowAsync <ArgumentException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            await action.Should().NotThrowAsync <InvalidOperationException>();
        }
        public async void When_async_method_throws_async_expected_exception_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Func <Task> action = async() => await asyncObject.ThrowAsync <ArgumentException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            await action.Should().ThrowAsync <ArgumentException>();
        }
        public async Task When_subject_throws_expected_async_exact_exception_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Func <Task> action = async() => await asyncObject.ThrowAsync <ArgumentException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            await action.Should().ThrowExactlyAsync <ArgumentException>("because {0} should do that", "IFoo.Do");
        }
        public async Task When_async_method_does_not_throw_async_exception_and_that_was_expected_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Func <Task> action = async() => await asyncObject.SucceedAsync();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            await action.Should().NotThrowAsync();
        }
        public void When_asserting_async_void_method_should_not_throw_specific_exception_it_should_fail()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var    asyncObject     = new AsyncClass();
            Action asyncVoidMethod = async() => await asyncObject.IncompleteTask();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncVoidMethod.Should().NotThrow <ArgumentException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().Throw <InvalidOperationException>("*async*void*");
        }
        public void When_async_method_throws_expected_exception_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncObject
                            .Awaiting(x => x.ThrowAsync <ArgumentException>())
                            .Should().Throw <ArgumentException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().NotThrow();
        }
        public void When_async_method_succeeds_and_expected_not_to_throw_particular_exception_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncObject
                            .Awaiting(x => asyncObject.SucceedAsync())
                            .Should().NotThrow <InvalidOperationException>();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().NotThrow();
        }
        public async Task When_subject_throws_subclass_of_expected_async_exact_exception_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Func <Task> action = async() => await asyncObject.ThrowAsync <ArgumentNullException>();

            Func <Task> testAction = async() => await action.Should().ThrowExactlyAsync <ArgumentException>("ABCDE");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            (await testAction.Should().ThrowAsync <XunitException>()).WithMessage("*ArgumentException*ABCDE*ArgumentNullException*");
        }
        public void When_async_method_does_not_throw_exception_and_that_was_expected_it_should_succeed()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncObject
                            .Awaiting(x => x.SucceedAsync())
                            .Should().NotThrow();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().NotThrow();
        }
        public void When_async_method_does_not_throw_expected_exception_it_should_fail()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncObject
                            .Awaiting(x => x.SucceedAsync())
                            .Should().Throw <InvalidOperationException>("because {0} should do that", "IFoo.Do");

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().Throw <XunitException>()
            .WithMessage("Expected System.InvalidOperationException because IFoo.Do should do that, but no exception was thrown*");
        }
        public void When_async_method_throws_exception_and_no_exception_was_expected_it_should_fail()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var asyncObject = new AsyncClass();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action action = () => asyncObject
                            .Awaiting(x => x.ThrowAsync <ArgumentException>())
                            .Should().NotThrow();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            action.Should().Throw <XunitException>()
            .WithMessage("Did not expect any exception, but found a System.ArgumentException*");
        }