問題描述
如何在 Rails 測試中斷言圖像源 (How to assert image source in rails test)
我的頁面應該包含一個類似於
<img src="some_long_url">
的圖像 您將如何使用 assert_select 進行測試? 我想使用 檢查圖像是否存在src="some long url ".
我已經試過了:
assert_select "img[src=?]", /amazon.com/
因為源的url是動態的,除了amazon.com 這就是為什麼我希望我的 src 屬性包含 amazon.com 並忽略保留正則表達式。
這意味著我希望我的 src 包含一些特定的字符串。 但它給出了以下 錯誤:
預計至少有 1 個元素匹配“img[src="(?‑mix:amazon.com)"]”,找到 0
我正在使用 Rails 4.2 並使用內置的測試框架。
參考解法
方法 1:
How about:
assert_select "img" do
assert_select "[src=?]", /amazon/
end
? From the docs