不带字符串的单元测试 MVC3 Razor 帮助器/视图
我正在尝试 MVC3 Razor 视图引擎,我正在探索的功能之一是对视图进行单元测试的能力。
我见过许多可以将 Razor 视图编译为程序集并呈现为字符串的示例。问题是它呈现为字符串,所以我只能进行字符串搜索!
这就是我正在尝试做的事情。
- 创建 Razor 帮助程序
- 编译帮助程序
- 运行已编译的帮助程序,传入 ViewModel
- 以某种 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.
- Create a Razor helper
- Compile helper
- Run compiled helper, passing in a ViewModel
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简而言之,答案是否定的,因为视图引擎的目的就是吐出字符串。将这些字符串解析为 XML 文档是一种赋予它们一定结构的方法,就像 @Craig-M 所建议的那样。但你必须问自己的是你真正测试的是什么。如果您的视图编译并生成某种 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:
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.
解决此问题的一种方法是将 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.
如何将视图渲染为 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.
看起来微软对于如何出于单元测试的目的,从视图生成 HTML。
It looks like Microsoft has its own answer of how to produce HTML from views, for the purpose of unit testing.