2012-10-13 31 views
2

我已經運行了幾個例子,其中人們用page.should have_selector "title", content: "my awsome content"來測試html元素的內容,但這似乎總是通過。正確的調用似乎是page.should have_selector "title", text: "my awsome content"(通知text:而不是content:)。Rspec內容與文本

這個Rspec調用中的content:選擇器是什麼?

+0

錯誤?儘管它可能取決於您使用的後端。 AFAIK Capybara和Webrat使用':text'並忽略':content',但我可能弄錯了。 –

回答

1

簡短的回答:

have_selector不支持:content選項。

龍答:

Rspec的創建轉換像[].should be_empty[].empty?.should == true,並在你的情況,page.should have_selector "title", content: "my awesome content"page.has_selector?("title, content: "my awesome content").should == true調用動態方法。

水豚提供了has_selector?方法,它基本上只是把它的參數,以它的test/unit風格斷言,assert_selector

def has_selector?(*args) 
    assert_selector(*args) 
rescue Capybara::ExpectationNotMet 
    return false 
end 

assert_selector然後再次通過其參數的水豚finder方法,all(),返回true若發現任何匹配,並提出例外,如果它不(這has_selector?捕獲,然後返回false與)。

def assert_selector(*args) 
    synchronize do 
    result = all(*args) 
    result.matches_count? or raise Capybara::ExpectationNotMet, result.failure_message 
    end 
    return true 
end 

all方法返回所有匹配查詢的結果。 This在這裏,我們將在可接受的選擇找到的文檔:

# @overload all([kind], locator, options) 
# @param [:css, :xpath] kind     The type of selector 
# @param [String] locator     The selector 
# @option options [String, Regexp] text  Only find elements which contain this text or match this regexp 
# @option options [Boolean] visible   Only find elements that are visible on the page. Setting this to false 
#            (the default, unless Capybara.ignore_hidden_elements = true), finds 
#            invisible _and_ visible elements. 
# @return [Array[Capybara::Element]]   The found elements 

我們可以看到,content不被列爲一個有效的選項,所以不理。

+1

由於評論最終沒有意義,這是需要檢查的代碼才能確定。 –