如何在 rspec 中测试 css 属性?
我正在使用 Rails 的 tabnav 插件,我想使用 rpsec 来确保它正确突出显示。
describe 'account navigation links' do
it 'should have account settings link' do
get '/account/settings'
response.should have_tag("li", :text => "Account Settings")
end
it 'should be highlighted' do
get '/account/settings'
response.should have_tag("li", :color => "Account Settings")
end
end
然而上面的代码似乎不起作用。我正在使用 webrat 和 rspec 顺便说一句。有什么帮助吗?谢谢。
I'm using tabnav plugin for Rails and I want to use rpsec to make sure it highlights properly.
describe 'account navigation links' do
it 'should have account settings link' do
get '/account/settings'
response.should have_tag("li", :text => "Account Settings")
end
it 'should be highlighted' do
get '/account/settings'
response.should have_tag("li", :color => "Account Settings")
end
end
However the above code doesn't seem to work. I'm using webrat with rspec btw. Any help? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里要测试的唯一真正的事情是是否应用特定的类名(如果突出显示来自类名)。如果是这样,您可以执行
have_tag("li.highlighted", :text => "Account Settings")
。否则,您可能不应该自动测试 CSS 选择器本身是否正确应用。这纯粹是演示性的细节,并不是测试套件真正要测试的内容。我怀疑 Webrat 不会费心为您检查和应用您的样式表,因此测试该细节是不可行的,更不用说您可以仅用一个页面加载来检查它是否正常工作 - 毕竟,您是可以说,在设计时测试样式表。
反正。您的问题并没有真正说明您真正想要测试的内容,但无论如何您不应该测试演示文稿。测试 HTML 文档的结构固然很好,但确认客户端程序如何解释该文档是设计师的职责,而不是程序员的职责。 (如果你身兼数职,那就这样吧,但不要混合你的食物。)
The only real thing to be testing here is whether or not a particular class name is applied, if highlighting comes from a class name. If so, you could do
have_tag("li.highlighted", :text => "Account Settings")
.Otherwise, you probably should not be automating your testing for whether or not the CSS selectors themselves are applied correctly. This is a purely presentational detail, and it isn't really what a test suite is designed to test. I suspect that Webrat doesn't bother to go through and apply your stylesheet for you, so testing that detail isn't feasible, not to mention that you could check with just one page load whether or not it's working - after all, you are arguably testing your stylesheet as you design it.
Anyway. Your question doesn't really make clear what you're really trying to test for, but you shouldn't be testing presentation, anyway. Testing the structure of the HTML document is good, but confirming how the client program interprets the document is the role of a designer, not a programmer. (If you wear both hats, so be it, but don't go mixing your foods.)
您可以编写更多测试,特别是对于“不突出显示错误操作”的情况,但我认为这已经足够好了。
You could write more test, especially for "doesn't highlight on wrong action" scenarios, but I think this is good enough.
如果您使用 Sass,则可以使用 Sass 解析器来解析它:
它返回一个解析树,您可以通过深入研究它来进行测试。例如:
If you're using Sass you can parse it with the Sass parser:
It returns a parse tree you could test by diving into it. For example: