Domanda

I have a super simple tempate page with a meta description on teh page. I want to write a test to validate that there is a single meta description and that the content is not empty.

This is what my test looks like at the moment:

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

It passes whether there is a meta description or not.

The page looks like this:

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

I am guessing that my CSS selector is invalid, anybody any ideas how to get the test working?

È stato utile?

Soluzione

It uses CsQuery under the hood so you can use CSS selectors like jQuery:

https://github.com/jamietre/CsQuery

Given your example I created a test route:

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

Then added a unit test:

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

The test passes:

enter image description here

Modify the name value:

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

And run the same test:

enter image description here

So the problem is just the selector you're using.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top