相关疑难解决方法(0)

使用XUnit断言异常

我是XUnit和Moq的新手.我有一个方法,它将字符串作为参数.如何使用XUnit处理异常.

[Fact]
public void ProfileRepository_GetSettingsForUserIDWithInvalidArguments_ThrowsArgumentException() {
    //arrange
    ProfileRepository profiles = new ProfileRepository();
    //act
    var result = profiles.GetSettingsForUserID("");
    //assert
    //The below statement is not working as expected.
    Assert.Throws<ArgumentException>(() => profiles.GetSettingsForUserID(""));
}
Run Code Online (Sandbox Code Playgroud)

正在测试的方法

public IEnumerable<Setting> GetSettingsForUserID(string userid)
{            
    if (string.IsNullOrWhiteSpace(userid)) throw new ArgumentException("User Id Cannot be null");
    var s = profiles.Where(e => e.UserID == userid).SelectMany(e => e.Settings);
    return s;
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing xunit

82
推荐指数
3
解决办法
5万
查看次数

标签 统计

c# ×1

unit-testing ×1

xunit ×1