Nunit async test exception assertion

后端 未结 6 1665
感情败类
感情败类 2020-12-29 18:37

[Edit (May 2020)] - This issue has been reportedly addressed in newer releases of NUnit. Please see Nunit.ThrowsAsync. (Ref this answer, thanks @James-Ross)

相关标签:
6条回答
  • 2020-12-29 19:04

    I'm not sure when it was added, but the current version of Nunit (3.4.1 at time of writing) includes a ThrowsAsync method

    see https://github.com/nunit/docs/wiki/Assert.ThrowsAsync

    Example:

    [Test]
    public void ShouldThrow404WhenNotFound()
    {
        var mockUserRepository = new Mock<IUserRepository>();
        mockUserRepository.Setup(x => x.GetByUserName(It.IsAny<string>())).Returns(default(User));
        var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
    
        var exception = Assert.ThrowsAsync<HttpResponseException>(() => userController.Get("foo"));
    
        Assert.That(exception.Response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
    }
    
    0 讨论(0)
  • 2020-12-29 19:05

    You're seeing problems due to async void.

    In particular:

    1. async () => await userController.Get("foo") is converted into TestDelegate, which returns void, so your lambda expression is treated as async void. So the test runner will begin executing the lambda but not wait for it to complete. The lambda returns before Get completes (because it's async), and the test runner sees that it returned without an exception.

    2. Wait wraps any exceptions in an AggregateException.

    3. Again, the async lambda is being treated as async void, so the test runner is not waiting for its completion.

    4. I recommend you make this async Task rather than async void, but in this case the test runner does wait for completion, and thus sees the exception.

    According to this bug report, there is a fix for this coming in the next build of NUnit. In the meantime, you can build your own ThrowsAsync method; an example for xUnit is here.

    0 讨论(0)
  • 2020-12-29 19:07

    I have a similar issue that you have in scenario 3 Test case failed due to following result

    Expected: <UserDefineException>
    But was:  null
    

    by using Assert.ThrowAsync<> problem gets solved

    My Web API action method and Unit test case method as below

    public async Task<IHttpActionResult> ActionMethod(RequestModel requestModel)
    {
       throw UserDefineException();
    }
    
    
    [Test]
    public void Test_Contrller_Method()
    {
       Assert.ThrowsAsync<UserDefineException>(() => _controller.ActionMethod(new RequestModel()));
    }    
    
    0 讨论(0)
  • 2020-12-29 19:21

    If you await a Task then Exceptions that are thrown are aggregated into AggregateException. You can inspect the inner exceptions of AggregateException. This could be the reason why you case 2 does not work.

    Unhandled exceptions that are thrown by user code that is running inside a task are propagated back to the joining thread, except in certain scenarios that are described later in this topic. Exceptions are propagated when you use one of the static or instance Task.Wait or Task.Wait methods, and you handle them by enclosing the call in a try-catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, then multiple exceptions could be thrown. To propagate all the exceptions back to the calling thread, the Task infrastructure wraps them in an AggregateException instance. The AggregateException has an InnerExceptions property that can be enumerated to examine all the original exceptions that were thrown, and handle (or not handle) each one individually. Even if only one exception is thrown, it is still wrapped in an AggregateException.

    Link to MSDN

    0 讨论(0)
  • 2020-12-29 19:26

    This blog talks about problems similar to mine.

    I followed the recommendation proposed there, and have a test like this -

        [Test]
        public void ShouldThrow404WhenNotFound()
        {
            var mockUserRepository = new Mock<IUserRepository>();
            mockUserRepository.Setup(x => x.GetByUserName(It.IsAny<string>())).Returns(default(User));
            var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
    
            var aggregateException = Assert.Throws<AggregateException>(() => userController.Get("foo").Wait());
            var httpResponseException = aggregateException.InnerExceptions
                .FirstOrDefault(x => x.GetType() == typeof(HttpResponseException)) as HttpResponseException;
    
            Assert.That(httpResponseException, Is.Not.Null);
            Assert.That(httpResponseException.Response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
        }
    

    I am not too pleased with it, but this works.

    EDIT 1

    Inspired by @StephenCleary, I added a static helper class that does the asserts that I am looking for. It looks like this -

    public static class AssertEx
    {
        public static async Task ThrowsAsync<TException>(Func<Task> func) where TException : class
        {
            await ThrowsAsync<TException>(func, exception => { });
        } 
    
        public static async Task ThrowsAsync<TException>(Func<Task> func, Action<TException> action) where TException : class
        {
            var exception = default(TException);
            var expected = typeof(TException);
            Type actual = null;
            try
            {
                await func();
            }
            catch (Exception e)
            {
                exception = e as TException;
                actual = e.GetType();
            }
    
            Assert.AreEqual(expected, actual);
            action(exception);
        }
    }
    

    I can now have a test like -

        [Test]
        public async void ShouldThrow404WhenNotFound()
        {
            var mockUserRepository = new Mock<IUserRepository>();
            mockUserRepository.Setup(x => x.GetByUserName(It.IsAny<string>())).Returns(default(User));
            var userController = new UserController(mockUserRepository.Object) { Request = new HttpRequestMessage() };
    
            Action<HttpResponseException> asserts = exception => Assert.That(exception.Response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
            await AssertEx.ThrowsAsync(() => userController.Get("foo"), asserts);
        }
    
    0 讨论(0)
  • 2020-12-29 19:28

    This is an example from documentation:

    var ex = Assert.ThrowsAsync<ArgumentException>(async () => await MethodThatThrows());
    
    1. Use ThrowsAsync
    2. Use async / await

    https://docs.nunit.org/articles/nunit/writing-tests/assertions/classic-assertions/Assert.ThrowsAsync.html

    0 讨论(0)
提交回复
热议问题