ASP.NET MVC - 测试页面到模型绑定

发布于 2024-07-27 17:46:10 字数 643 浏览 8 评论 0原文

我有涵盖我的模型活页夹的单元测试。 我创建一个 ModelBindingContext 并使用我的测试值填充 ValueProviderDictionary。 我相信,一旦我的控制器获得模型,一切都经过测试,并且正确的事情正在发生。 我也相信,如果 BindingContext 正确,我的模型将正确绑定。 我不确信的是,我的 View 在回发时会正确填充 BindingContext。

我已经开始编写 WatiN 测试来填写表单、将其发回并检查生成的视图。 这没关系,但看起来很……间接。 例如,单击提交后显示“您的订单已保存”的页面 - 我必须访问数据库以查看它是否已正确保存。 我已经进行了测试,涵盖当传递到我的控制器的模型为 x 时,y 被保存到数据库中。 为什么我要在视图测试中重复此代码?

我真正想要的是填充视图,点击提交,然后检查 (a) BindingContext 或 (b) 从 ModelBinder 返回的模型。

有没有关于如何做这些事情的实践?


只是想补充一点,这将是 WatiN 测试的补充。 我现在有大约 50 个 WatiN 测试(还有更多需要添加),如果我可以在没有 WatiN 的情况下执行上述操作,那么只会删除少数测试。 我会继续使用 WatiN,因为它有好处,但我觉得我现在在这几个测试中滥用了它。

I have unit tests covering my model binders. I create a ModelBindingContext and populate the ValueProviderDictionary with my test values. I feel confident that once my controller gets the model, everything is covered with testing and the right things are happening. I also feel confident that if the BindingContext is correct my model will bind correctly. What I do not feel confident on is that my View, when posted back, will populate the BindingContext properly.

I've started to write WatiN tests that fill out forms, post them back, and check the resulting View. This is okay, but seems very ... indirect. For instance a page that says "your order was saved" after clicking submit -- I would have to go at the database to see if it was saved correctly. I already have tests covering that when the model passed to my controller is x, y is saved to the database. Why am I repeating this code in a view test?

What I really want is to populate the view, hit submit, and check either (a) the BindingContext or (b) the model returned from the ModelBinder.

Is there any practices on how to do this stuff?


Just wanted to add that this would be in addition to WatiN tests. I have ~50 WatiN tests right now (with plenty more to add), and if I could do the above without WatiN it would only remove a handful of tests. I'll keep using WatiN for what it is good for, but I feel like I'm abusing it right now for those few tests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-08-03 17:46:10

在您的情况下,只有当您的视图无法遵循框架约定时,模型绑定才会失败。 发生这种情况的第一个迹象是您的视图没有显示预期结果。 下一个可能是当您的 WatiN 测试在帖子上失败时。

从技术上讲,是的,这是间接测试,但也是最务实的一种。 除了观察它们是否有效之外,您还如何验证所有观点都遵守惯例?

In your case, the model binding will only fail when your views fail to follow the framework conventions. The first sign of that happening, is when your view doesn't show the expected result. The next one would probably be when your WatiN tests fail on posts.

Technically, yes, this is indirect testing, but also the most pragmatic one. How would you else verify that all views adhere to the conventions, other than observe that they work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文