Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.ThrowsExceptionAsync isn't working

Question:

I haven't found much about MSTest V2 for examples or documentation. What is the correct way to use Assert.ThrowsExceptionAsync?

public void GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
    Assert.ThrowsException<PlaylistNotFoundException>(async () =>
    {
       await manager.GetPlaylistByIdAsync(playlistId);
    });
}

this also fails the test:

Assert.ThrowsException<PlaylistNotFoundException>(() =>
{
    return manager.GetPlaylistByIdAsync(playlistId);
});

Message: Assert.ThrowsException failed. No exception thrown. PlaylistNotFoundException exception was expected.

This is failing for me, even though I've debugged it and the exception is definitely thrown.

Since this is still an RC, it's possible there is a bug. I've had this in 2 tests I'm trying to convert so I can use VS 2017.

Update: This passes.

[TestMethod]
public async Task GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
    //Assert.ThrowsException<PlaylistNotFoundException>(() =>
    //{

    try
    {
        await manager.GetPlaylistByIdAsync(playlistId);
        Assert.Fail();
    }
    catch (PlaylistNotFoundException)
    {
        Assert.IsTrue(true);
    }

    //});
}

Update 2: After Stephen Cleary's answer, I made this change. Thanks for pointing out my mis-use. I had changed it awhile back because I get "Message: Test method .Test.Models.Helpers.PlaylistTargetFusionManagerTests.GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException threw exception: System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task1<!!0> Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func1<System.Threading.Tasks.Task>)'." when I run the test.

[TestMethod]
[TestCategory(TestCategories.CSharp)]
[TestCategory(TestCategories.PlaylistTargets)]
public async Task GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
    await Assert.ThrowsExceptionAsync<PlaylistNotFoundException>(() => manager.GetPlaylistByIdAsync(playlistId));
}

I have 2 packages in my packages.json

 <package id="MSTest.TestAdapter" version="1.1.9-rc2" targetFramework="net451" />
 <package id="MSTest.TestFramework" version="1.0.8-rc2" targetFramework="net451" />
like image 365
AlignedDev Avatar asked Jan 12 '17 19:01

AlignedDev


2 Answers

What is the correct way to use Assert.ThrowsExceptionAsync?

You're not calling ThrowsExceptionAsync. You're calling ThrowsException. The proper way to call ThrowsExceptionAsync is to await its result.

This should work:

public async Task GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
  var playlistId = Guid.NewGuid().ToString();
  var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();
  await Assert.ThrowsExceptionAsync<PlaylistNotFoundException>(async () =>
  {
     await manager.GetPlaylistByIdAsync(playlistId);
  });
}

or, more simply:

  await Assert.ThrowsExceptionAsync<PlaylistNotFoundException>(() =>
     manager.GetPlaylistByIdAsync(playlistId));
like image 59
Stephen Cleary Avatar answered Nov 12 '22 05:11

Stephen Cleary


Because your method is async you can try something like this, that also allows you to test the values of the raised exception:

public void GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException()
{
    var playlistId = Guid.NewGuid().ToString();
    var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager();

    var ex = Assert.ThrowsExceptionAsync<PlaylistNotFoundException>(
            async () =>
            {
                await manager.GetPlaylistByIdAsync(playlistId);
            }).Result;

    Assert.IsTrue(ex.ErrorCode == ...);
}
like image 2
Miguel Domingues Avatar answered Nov 12 '22 04:11

Miguel Domingues