2010-05-15 72 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')