2013-04-08 38 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