I have a unit test that attempts to sign-in with incorrect credentials and checks the resulting response body for some specific 'errorbox' html. This works just fine.
[Fact]
public void SignIn__Should_display_error_message_when_error_passed()
{
var browser = Fake.Browser();
var response = browser.Get("/signin", with => with.Query("error", "true"));
response.Body["#errorBox"]
.ShouldExistOnce()
.And.ShouldBeOfClass("floatingError")
.And.ShouldContain("invalid", StringComparison.InvariantCultureIgnoreCase);
}
My pages were getting a bit repetitious so I re-organised things to create a simple set of nested razor views, like this:
The SignIn View
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
Layout = "_Master.cshtml";
}
<h3>Sign In</h3>
<form class="nice" method="POST">
... [labels, inputs etc]
</form>
@if (Model.HasError)
{
<div id="errorBox" class="floatingError">Invalid UserName or Password</div>
}
The _Master View
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@Model.Title</title>
... [css javascript etc]
@RenderSection("Head", required: false)
</head>
<body>
@RenderBody()
</body>
</html>
This still works and my unit test still passes (bear with me). Time passes and the code gets more complex, so now I need to reorganise again. I have added another layer of nesting to my razor views.
The _Page View
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
Layout = "_Master.cshtml";
}
@section Head{
[... shared javascript, css]
@RenderSection("Head", required: false)
}
[.. some shared content]
@RenderBody()
The double nesting looks like this:
SignIn -> _Page -> _Master
Now my unit test fails as the response does not contain the correct html. Running the code in an real browser (chrome) and manually supplying incorrect credentials produces the correct response, it is only the unit-test that fails.
Drilling down to response.Body.responseDocument.agilityPackDocumentInternal.DocumentNode.InnerHtml
shows this exception:
Error Compiling Template: (15, 11) The type or namespace name 'XXXX' could not be found
Re-pointing the SignIn view back to using _Master as its layout (cutting out the middle _Page view) makes the Unit Test pass again.
Yet the _Page view does nothing code-wise, it just adds some <script>
and <style>
tags and passes on the baton. To test if those tags are the problem, I shifted them to the _Master view and kept the nesting at one level (SignIn -> _Master) and the test passed.
So, does Nancy.Testing
support two levels of razor layout nesting? Otherwise, can anybody spot my mistake?
Thank You
P.S. I don't know if it is connected but I suspected that view caching might be the problem (after reading Test driving Nancy Modules) I checked the value of Nancy.StaticConfiguration.DisableCaches
and sure enough, it was set to false
(despite my DEBUG
symbol being switched on and running the test in Debug mode). I set this value to true in my custom unit test BootStrapper class but this made no difference.
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
Nancy.StaticConfiguration.DisableCaches = true;
}
The answer to this question can be found in my follow up question:
Why does Nancy.Testing fail when @using statements are included in razor layouts?