不带字符串的单元测试 MVC3 Razor 帮助器/视图

发布于 2024-10-14 20:42:14 字数 435 浏览 6 评论 0原文

我正在尝试 MVC3 Razor 视图引擎,我正在探索的功能之一是对视图进行单元测试的能力。

我见过许多可以将 Razor 视图编译为程序集并呈现为字符串的示例。问题是它呈现为字符串,所以我只能进行字符串搜索!

这就是我正在尝试做的事情。

  1. 创建 Razor 帮助程序
  2. 编译帮助程序
  3. 运行已编译的帮助程序,传入 ViewModel
  4. 以某种 HTML/XML/树结构形式获取帮助程序的输出

我想要这样做的原因是这样我可以测试输出的特定部分。帮助器可能会输出包含各种输出内容的 HTML。我想做的是查看是否有一个具有特定值的复选框(例如)。如果您看过 Selenium 测试,它与我想做的类似,只是不是作为服务器驱动的测试。

有没有办法让编译后的 Razor(或其他视图引擎)视图发出字符串以外的东西?

I am trying out the MVC3 Razor view engine, and one the features that I am exploring is the ability to unit test views.

I have seen a number of examples where Razor views can be compiled into an assembly and rendered into a string. The problem is that it renders as a string, so I am left with doing string searches!

Here is what I am trying to do.

  1. Create a Razor helper
  2. Compile helper
  3. Run compiled helper, passing in a ViewModel
  4. Get the output of the helper as some sort of HTML/XML/tree structure

The reason that I want to do this is so that I can test for specific parts of the output. The helper will likely spit out HTML that includes various output gunk. What I want to do is to see if there is a Checkbox with a particular value (for example). If you have seen Selenium tests, it is similar to what I would like to do, except not as server driven tests.

Is there some way to get compiled Razor (or other view engine) views to emit something other than strings?

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

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

发布评论

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

评论(4

铃予 2024-10-21 20:42:14

简而言之,答案是否定的,因为视图引擎的目的就是吐出字符串。将这些字符串解析为 XML 文档是一种赋予它们一定结构的方法,就像 @Craig-M 所建议的那样。但你必须问自己的是你真正测试的是什么。如果您的视图编译并生成某种 HTML,则它生成的内容可能会存在三个问题:

  • 它显示的数据不正确。您将在控制器级别对此进行测试,因此您可以在视图测试期间忽略它。
  • MVC 框架出现错误,生成了错误的 HTML 字符串。您不必担心这一点,因为 MVC 有自己的测试套件,并且由于关注点分离,这不是您的问题。
  • HTML 破坏了用户界面。

最后一个测试非常好,但为什么不在 javascript 单元测试中测试它呢?查看流行的 javascript 单元测试套件,如 JsUnit、FireUnit、QUnit 等。它们在解析 Razor 输出方面都会比您做得更好。

我认为分配给复选框的值可能会在控制器测试中进行测试。但要使用您的示例,测试复选框的值可以是 $('#theCheck').val() == 'the value'。您可以针对使用依赖项注入存储库或服务运行的网站运行这些内容,以控制“值”等内容。

只是一个想法。

The short answer is no, because view engines' purpose in life is to spit out strings. Parsing those strings into an XML document is a way to give them a little structure, like @Craig-M suggested. But what you have to ask yourself is what you're really testing. If your view compiles and generates some kind of HTML, there can be three problems with what it generated:

  • the data it displays is not correct. You would test for this at the controller level, so you can ignore it during view testing.
  • the MVC framework had an error and it generated the wrong HTML string. You don't worry about this, because MVC has its own test suite and thanks to separation of concerns, it's not your problem.
  • the HTML has broken the user interface.

That last one would be very nice to test for, but why not test for it in javascript unit tests? Check out popular javascript unit test suites like JsUnit, FireUnit, QUnit, etc. They would all do a much better job than you could do of parsing out Razor output.

I think the value assigned to a checkbox would probably be tested for in Controller testing. But to use your example, testing your checkbox's value could be $('#theCheck').val() == 'the value'. And you can run these against a website running with dependency injected repositories or services to control things like 'the value'.

Just a thought.

情丝乱 2024-10-21 20:42:14

解决此问题的一种方法是将 html 字符串解析为 XDocument 并让程序集返回它。然后您可以在测试中使用 LINQ 对其进行查询。

编辑:我也在尝试找出 Razor 测试策略。我很想知道如何让助手工作。到目前为止,我在将它们呈现为 MVC 框架之外的字符串方面还处于空白。

One way you could go about this is to parse the html string into an XDocument and have your assembly return it. Then you can query against it with LINQ in your tests.

Edit: I'm trying to figure out a Razor test strategy too. I'd be interested to know how you get the helper to work. So far, I'm drawing a blank in getting them rendered to strings outside of the MVC framework.

万水千山粽是情ミ 2024-10-21 20:42:14

如何将视图渲染为 html,然后将该 html 发送到 HtmlAgility 库?这样您就可以轻松地遍历/导航您的 html。我认为 MVC 本身没有任何方法可以做到这一点。

How about rendering your views to html and then sending that html to HtmlAgility library? That way you can easily traverse/navigate your html. I don't think there is any way to do that with MVC itself.

这个俗人 2024-10-21 20:42:14

It looks like Microsoft has its own answer of how to produce HTML from views, for the purpose of unit testing.

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