我想測試從我的南希應用程序返回的模型是否如預期。我按照文檔here,但每當我打電話給GetModel<T>
擴展方法它會拋出一個KeyNotFoundException
。南希測試GetModel <T>拋出KeyNotFoundException
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
我知道錯誤的含義,但我不明白爲什麼它被拋出。
這裏是我的模塊
public class SanityModule : NancyModule
{
public SanityModule()
{
Get["sanity-check"] = _ => Negotiate.WithModel(new SanityViewModel { Id = 1 })
.WithStatusCode(HttpStatusCode.OK);
}
}
我的視圖模型
public class SanityViewModel
{
public int Id { get; set; }
}
,這裏是我的測試
[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);
}
}
調試這個測試表明,該模塊被擊中並完成就好了。運行該應用程序顯示響應如預期。
任何人都可以對此有所瞭解嗎?
我相信'GetModel'是爲了讓模型在渲染的html視圖背後。你可以將json反序列化到SanityModel來檢查它。 –
albertjan
2014-09-03 20:16:06