使用Assert.ThrowsExceptionAsync的正确方法是什么?
你没打电话ThrowsExceptionAsync
.你在打电话ThrowsException
.调用的正确方法ThrowsExceptionAsync
是await
结果.
这应该工作:
public async Task GetPlaylistByIdAsync_NonExistingPlaylist_ThrowsPlaylistNotFoundException() { var playlistId = Guid.NewGuid().ToString(); var manager = PlaylistTargetsFakeFactory.GetPlaylistTargetFusionManager(); await Assert.ThrowsExceptionAsync(async () => { await manager.GetPlaylistByIdAsync(playlistId); }); }
或者,更简单地说:
await Assert.ThrowsExceptionAsync(() => manager.GetPlaylistByIdAsync(playlistId));