2013-12-20 19 views
3

我有一個超級簡單的tempate頁面,在頁面上有元描述。我想編寫一個測試來驗證是否存在單個元描述並且內容不是空的。Nancy:我如何測試一個元描述標記

這是我的測試看起來像此刻:

[Test] 
public void Get_Root_Should_Return_Page_With_Meta_Description() 
{ 
    // Given 
    var browser = new Browser(new Bootstrapper()); 

    // When 
    var result = browser.Get("/"); 

    // Then 
    result.Body["@description [content]"].ShouldExistOnce(); 
} 

它通過是否有meta描述與否。

的頁面看起來是這樣的:

<!DOCTYPE html> 
<html> 
<head> 
    <title>@Model.Title</title> 
    <meta charset="utf-8" /> 
    <meta name="description" content="@Model.MetaDescription" /> 

我猜測,我的CSS選擇器是無效的,任何人任何想法如何得到測試的工作?

回答

3

它採用CsQuery引擎蓋下,所以你可以使用CSS選擇器如jQuery:

https://github.com/jamietre/CsQuery

鑑於您的例子中,我創建了一個測試路線:

Get["description"] = _ => @"<!DOCTYPE html> 
<html> 
<head> 
    <title>@Model.Title</title> 
    <meta charset=""utf-8"" /> 
    <meta name=""description"" content=""@Model.MetaDescription"" /> 
</head> 
<body> 
</body> 
</html>"; 

然後添加一個單元測試:

[Fact] 
public void Get_Root_Should_Return_Page_With_Meta_Description() 
{ 
    // Given 
    var browser = new Browser(x => x.Module<TestModule>()); 

    // When 
    var result = browser.Get("/description"); 

    // Then 
    result.Body["meta[name=description]"].ShouldExistOnce(); 
} 

測試通過:

enter image description here

修改名稱值:

<meta name=""BANANA"" content=""@Model.MetaDescription"" />

並運行相同的測試:

enter image description here

所以,問題就是你使用的選擇。