2013-04-08 39 views
2

有人可以解釋我爲什麼不能通過測試嗎? 我只是用html內容測試文章視圖。 鑑於我在我的測試顯示通過html_safeRspec - 查看測試和html_safe

.article_content 
    = @article.content.html_safe 

內容我有類似如下:

context 'shows actual article' do 
     before(:all) { @article = FactoryGirl.build(:article, id: 1, 
     content: '<h2>aaaaa ddd bbb</h2>') } 

     before(:each) { render } 

    it 'show content' do 
     render.should have_selector('div.article_content',text: @article.content.html_safe) 
    end 

,我的測試失敗後:

Failure/Error: render.should have_selector('div.article_content',text: @article.content.html_safe) 
    Capybara::ExpectationNotMet: 
     expected to find css "div.article_content" with text "<h2>aaaaa ddd bbb</h2>" 
but there were no matches. Also found "\naaaaa ddd bbb\n", which matched the selector but not all filters. 

,但是當我放棄html標籤從工廠對象的內容測試通過。 我不知道爲什麼html標籤要'\ n'。在瀏覽器中都很好看。

回答

1

的has_selector/have_selector匹配上這實際上是可見的文本匹配 - 這顯示了網頁上的文字aaaaa ddd bbb而非<h2>aaaaa ddd bbb</h2>'

+0

行,我明白了,謝謝你。但是有沒有一種方法可以從構建的對象中剝離標籤來進行測試?或者我只能手動進行。 – Karol85 2013-04-09 05:11:18