好吧,似乎没有现成的支持,我写了这个自定义匹配器:
RSpec::Matchers.define :match_exactly do |expected_match_count, selector|
match do |context|
matching = context.all(selector)
@matched = matching.size
@matched == expected_match_count
end
failure_message_for_should do
"expected '#{selector}' to match exactly #{expected_match_count} elements, but matched #{@matched}"
end
failure_message_for_should_not do
"expected '#{selector}' to NOT match exactly #{expected_match_count} elements, but it did"
end
end
现在,您可以执行以下操作:
describe "initial page load", :type => :request do
it "has 12 inputs" do
visit "/"
page.should match_exactly(12, "input")
end
end
并得到如下输出:
1) initial page load has 12 inputs
Failure/Error: page.should match_exactly(12, "input")
expected 'input' to match exactly 12 elements, but matched 13
现在就成功了,我将研究制作这部分的水豚。