Skip to main content
 首页 » 编程设计

c#之NUnit 是否支持 C# 动态的流畅语法

2024年02月23日13jyk

我有一个 nUnit 测试用例断言从 Facebook C# SDK 返回的动态。有什么方法可以使用 NUnit 流畅的语法来断言它。这是我正在寻找的非常简化的示例:

// not working 
var client = new FacebookClient(accessToken); 
dynamic userInfo = client.Get("me"); 
Assert.That(userInfo, Is.Not.Null); 
Assert.That(userInfo, Has.Property("id").Not.Null); 
Assert.That(userInfo, Has.Property("name").Not.Null); 

现在我只能通过直接指定'properties'来测试

// working, but not fluent 
var client = new FacebookClient(accessToken); 
dynamic userInfo = client.Get("me"); 
Assert.That(userInfo, Is.Not.Null); 
Assert.That(userInfo.id, Is.Not.Null); 
Assert.That(userInfo.name, Is.Not.Null); 

感谢您的回答。

请您参考如下方法:

json 对象是 IDictionary 所以你可以使用它的方法。

var client = new FacebookClient(accessToken); 
dynamic userInfo = client.Get("me"); 
var hasId = userInfo.ContainsKey("id"); 

我没有使用过 nunit fluent api,但我猜可能有一些方法,比如 Has.Key()