2010-05-15 73 views
3

我使用tabnav插件Rails和我想用rpsec,以确保它突出了正常。如何在rspec中测试css属性?

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 

但是上面的代码似乎不起作用。我使用webpec与rspec btw。任何帮助?谢谢。

回答

2
describe 'highlighting' do 
    it 'should highlight account/settings' do 
    get '/account/settings' 
    response.should have_tag("a.active[href=?]", account_settings_path, /Account Settings/i) 
    end 

    it 'should highlight account/profile' do 
    get '/account/profile' 
    response.should have_tag("a.active[href=?]", account_profile_path, /Profile Information/i) 
    end 

    it 'should highlight account/picture' do 
    get '/account/picture' 
    response.should have_tag("a.active[href=?]", account_picture_path, /Profile Picture/i) 
    end 

    it 'should highlight account/notifications' do 
    get '/account/notifications' 
    response.should have_tag("a.active[href=?]", account_notifications_path, /Notifications/i) 
    end 

    it 'should not highlight Profile' do 
    get '/account/profile' 
    response.should_not have_tag("a.active[href=?]", account_settings_path, /Account Settings/i) 
    end 

    it 'should not highlight Notifications' do 
    get '/account/profile' 
    response.should_not have_tag("a.active[href=?]", account_notifications_path, /Notifications/i) 
    end 

    it 'should not highlight Picture' do 
    get '/account/profile' 
    response.should_not have_tag("a.active[href=?]", account_picture_path, /Profile Picture/i) 
    end 
end 

你可以写更多的测试,特别是对场景“不会对错误的行动突出”,但我认为这已经足够了。

4

唯一真实的东西在这里测试的是一个特定的类名是否被应用,如果突出显示来自一个类名。如果是这样,你可以做have_tag("li.highlighted", :text => "Account Settings")

否则,你可能不应该自动化您为自己的正确应用CSS选择器是否测试。这只是一个纯粹的表达细节,并不是测试套件设计要测试的内容。我怀疑Webrat不打扰经历和应用样式表给你,所以测试一个细节是不可行的,更何况,你可以只用一个页面加载与否它的工作检查 - 毕竟,你是可以说是测试你的样式表为您设计它

无论如何。你的问题并没有明确说明你真的想要测试什么,但是不管怎样,你都不应该测试演示文稿。测试HTML文档的结构是好的,但确认客户端程序如何解释文档是设计师的角色,而不是程序员。 (如果你戴的帽子都,就这样吧,但不要混合你的食物。)

+0

我想测试它的原因是因为它是Rails插件(tabnav),而不是纯粹的设计。如果你在这里看一下这个pastie http://pastie.org/961207,我想你会明白我为什么要测试它。 要回答你的问题,我想测试它是否突出显示在正确的页面上。 tabnav的工作方式是,如果我在某个控制器和操作上,它会突出显示/更改特定链接的颜色。谢谢。 – 2010-05-15 01:49:32

+0

我怀疑tabnav应用CSS类。拉起HTML文档,查看类是什么,然后测试它:)这种情况将归于此答案的第一段HTML结构,而不是最后两段。 – Matchu 2010-05-15 01:53:10

+0

...其实,现在我想通过这个,你应该甚至是测试tabnav的基本功能?如果它已经过全面测试,只需信任它。 – Matchu 2010-05-15 01:57:06

1

如果您使用的Sass你可以用无礼的话解析器解析它:

root = Sass::SCSS::Parser.new('.error { color: red; }', 'example.scss').parse 

它返回一个解析树,你可以通过潜水考进去。例如:

prop = root.children.select {|child| child.rule.flatten.include?('.error')}.first 
prop_strings = prop.children.map {|p| [p.name.flatten.first, p.value].join(':')} 
prop_strings.should include?('color:red')