Nancy Testing GetModel <T> Throws a KeyNotFoundException

I am trying to verify that the model returned from my Nancy application is as expected. I followed the docs here , but whenever I call the extension method GetModel<T>, it throws KeyNotFoundException.

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

I know what error means, but I do not understand why it was thrown.

Here is my module

public class SanityModule : NancyModule
{
    public SanityModule()
    {
        Get["sanity-check"] = _ => Negotiate.WithModel(new SanityViewModel { Id = 1 })
                                            .WithStatusCode(HttpStatusCode.OK);
    }
}

my view model

public class SanityViewModel
{
    public int Id { get; set; }
}

and here is my test

[TestFixture]
public class SanityModuleTests
{
    [Test]
    public void Sanity_Check()
    {
        // Arrange
        var browser = new Browser(with =>
        {
            with.Module<SanityModule>();
            with.ViewFactory<TestingViewFactory>();
        });

        // Act
        var result = browser.Get("/sanity-check", with =>
        {
            with.HttpRequest();
            with.Header("accept", "application/json");
        });
        var model = result.GetModel<SanityViewModel>();

        // Asset
        model.Id.ShouldBeEquivalentTo(1);
    }
}

Debugging this test shows that the module hit and completed just fine. Running the application shows that the answer is as expected.

Can anyone shed some light on this?

+4
source share
1 answer

albertjan the.fringe.ninja , .

TL; DR , . .

, application/json, TestingViewFactory.

GetModel<T>();

public static TType GetModel<TType>(this BrowserResponse response)
{
    return (TType)response.Context.Items[TestingViewContextKeys.VIEWMODEL];
}

NancyContext . , NancyContext . , NancyContext RenderView TestingViewFactory.

public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext)
{
    // Intercept and store interesting stuff
    viewLocationContext.Context.Items[TestingViewContextKeys.VIEWMODEL] = model;
    viewLocationContext.Context.Items[TestingViewContextKeys.VIEWNAME] = viewName;
    viewLocationContext.Context.Items[TestingViewContextKeys.MODULENAME] = viewLocationContext.ModuleName;
    viewLocationContext.Context.Items[TestingViewContextKeys.MODULEPATH] = viewLocationContext.ModulePath;

    return this.decoratedViewFactory.RenderView(viewName, model, viewLocationContext);
}

json, RenderView . , GetModel<T>, html.

- api, ,

with.Header("accept", "application/json");

with.Header("accept", "text/html");

ViewNotFoundException. , IViewFactory. ( the.fringe.ninja)

public class TestViewFactory : IViewFactory
{
    #region IViewFactory Members

    public Nancy.Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext)
    {
        viewLocationContext.Context.Items[Fixtures.SystemUnderTest.ViewModelKey] = model;
        return new HtmlResponse();
    }

    #endregion
}

with.ViewFactory<TestingViewFactory>();

with.ViewFactory<TestViewFactory>();

GetModel<T> .

+3

All Articles