测试:如何测试该视图包含所需数据 [英] Testing: How to test that view contains desired data

查看:120
本文介绍了测试:如何测试该视图包含所需数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设厨师可以制作食谱,而副厨师可以创建必须得到主厨批准的食谱.

Say a Chef can make Recipes, and Sous-Chefs can create Recipes that must be approved by a Head Chef.

您想测试一下,当厨师长查看她的主页时,她看到自己创建的食谱.您还想测试一下她是否发现有等待她批准的食谱.

You want to test that, when a Head Chef views her homepage, she sees Recipes that she herself created. You also want to test that she sees there are Recipes awaiting her approval.

我可以想到两种方法:

  1. 测试视图中是否包含某些词,例如您的食谱"和等待您批准的食谱"
  2. 在正在使用的html元素中添加不必要的属性,以便您可以使用"id = recipe_1"或"data-for-the-sake-of-sing-of-testing = 1"检查元素.

我非常不喜欢这两种方法.

I very much dislike both of these approaches.

  1. 非常脆弱的测试.每次您想要对该副本进行较小的更新时,测试都会中断.
  2. i18n?这种方法将如何工作?

可能还有更多原因,但这两个原因非常大.

There are probably more reasons, but those two are pretty huge.

仅仅为了测试而拥有多余的标记是多么令人讨厌!为了测试,用户不应增加下载大小.

How annoying to have superfluous markup just for the sake of testing! The user should not have an increased download size for the sake of tests.

有什么好的方法?我很想听到您使用的任何语言的替代音.我主要考虑使用Ruby,Test :: Unit,Minitest,RSpec和Cucumber(尽管我的Cuke技能过时),但是如果使用其他语言/框架,弄清楚了这个,我也很想看看他们在做什么.

What is a good approach to this? I'm interested to hear any alternatives at all, in whatever language you think in. I mostly think in Ruby, Test::Unit, Minitest, RSpec, and Cucumber (though my Cuke skills are stale), but if other languages/frameworks have this figured out, I'd love to see what they're doing, too.

推荐答案

我至少看到两个选择:

  1. 避免通过UI测试业务逻辑.编写一个返回原始数据结构的服务"或用例控制器"对象.换句话说,您为系统构建了一个API.您的单元测试通过API访问系统.您的UI通过相同的API访问系统,但是视图中几乎没有逻辑.参见 http://www.confreaks.com/videos/759-rubymidwest2011-keynote-architecture-the-lost-years http://www.cleancoders.com/codecast/clean-code-episode-7/show .

使用"页面对象"模式.编写一个对象,该对象读取应用程序生成的HTML代码,对其进行解析并通过getter提供有趣的数据.这将使您的 test 代码清晰可见.您的反对意见可能是您仍然有第二个问题.实际上,我认为这不是真正的问题.如果使用结构化HTML标记,则提取所需信息应该相当容易.如果将ID附加到页面的关键元素上,将更加容易.在您的示例中,我将有一个id为"my-recipes"的div和另一个id为待批准"的div.那应该足够了;其他任何东西都应该可以通过xpath或CSS选择器轻松找到.您为什么觉得这令人反感?这些ID可能会用于其他目的,例如使用不引人注目的JavaScript附加行为或使用CSS样式表附加样式.

Use the "page object" pattern. Write an object that reads in the HTML code that your app produces, parses it, and makes interesting data available through getters. This will do wonders to make your test code clear. Your objection could be that you still have problem #2. In fact I don't think it's really a problem. If you use structural HTML markup, it should be fairly easy to extract the information you need. It will be much easier if you attach an ID to a key element of the page; in your example I would have a div with id="my-recipes" and another div with id="to-be-approved". That should be enough; anything else should be easy to find with xpath or css selectors. Why do you find this objectionable? These IDs will probably be useful for other purposes, such as attaching behaviour with unobtrusive JavaScript or attaching styles with a CSS stylesheet.

这篇关于测试:如何测试该视图包含所需数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆