gpt4 book ai didi

c# - Assert.ThrowsExceptionAsync 不工作

转载 作者:太空狗 更新时间:2023-10-29 20:53:57 27 4
gpt4 key购买 nike

问题:

我还没有找到很多关于 MSTest V2 的信息例如或documentation . Assert.ThrowsExceptionAsync 的正确使用方法是什么?

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

这也没有通过测试:

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

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

这对我来说是失败的,即使我已经调试它并且肯定会抛出异常。

由于这仍然是一个 RC,因此可能存在错误。我在尝试转换的 2 个测试中进行了此操作,因此我可以使用 VS 2017。

更新:这通过了。

[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);
}

//});
}

更新 2: 在 Stephen Cleary 的回答之后,我做了这个改变。感谢您指出我的误用。我已经更改了一段时间,因为我收到“消息:测试方法 .Test.Models.Helpers.PlaylistTargetFusionManagerTests.GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException 抛出异常:System.MissingMethodException:找不到方法:'System.Threading.Tasks.Task 1<!!0> Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync(System.Func 1 )'."当我运行测试时。

[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));
}

我的 packages.json 中有 2 个包

 <package id="MSTest.TestAdapter" version="1.1.9-rc2" targetFramework="net451" />
<package id="MSTest.TestFramework" version="1.0.8-rc2" targetFramework="net451" />

最佳答案

What is the correct way to use Assert.ThrowsExceptionAsync?

你没有调用 ThrowsExceptionAsync .您正在调用 ThrowsException .调用 ThrowsExceptionAsync 的正确方法是等待它的结果。

这应该有效:

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

或者,更简单地说:

  await Assert.ThrowsExceptionAsync<PlaylistNotFoundException>(() =>
manager.GetPlaylistByIdAsync(playlistId));

关于c# - Assert.ThrowsExceptionAsync 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com