我是新来测试多种可能的结果,所以忍耐一下:)水豚/ RSpec的 - 测试与have_content
我想测试一个网页上的随机内容。例如,让我们测试显示内容'foo'或'bar'的页面。
喜欢的东西下面就是我想要达到
page.should (have_content('foo') || have_content('bar'))
任何想法?
我是新来测试多种可能的结果,所以忍耐一下:)水豚/ RSpec的 - 测试与have_content
我想测试一个网页上的随机内容。例如,让我们测试显示内容'foo'或'bar'的页面。
喜欢的东西下面就是我想要达到
page.should (have_content('foo') || have_content('bar'))
任何想法?
我不知道任何准备好为您使用Mather。但你可以自己烘烤。
使用satisfy:
page.should satisfy {|page| page.has_content?('foo') or page.has_content?('bar')}
def have_foo_or_bar
simple_matcher("check content has foo or bar") { |page| page.has_content?('foo') or page.has_content?('bar') }
end
describe page
it "should have foo or bar" do
page.should have_foo_or_bar
end
end
编辑:has_content
接受正则表达式,所以你可以检查page.should have_content(/foo|bar/)
使用满意工作。无法让simple_matcher正常工作,因为它正在吐出NoMethodErrors。根据我的理解,该方法已被弃用?本来希望使用simple_matcher作为自定义错误消息。无论如何,感谢您的帮助! – Viet 2012-02-02 23:24:33
对,我给你的链接已经过时了。对不起:(不知道为什么谷歌显示在顶部,应该更新它:)当前版本是2.8 [RSpec期望2.8](https://www.relishapp.com/rspec/rspec-expectations)。与simple_matcher最接近的是[define matcher](https://www.relishapp.com/rspec/rspec-expectations/docs/custom-matchers/define-matcher)。 – 2012-02-03 04:58:00
我有同样的问题,但实现这是可能的更容易&增加一个if语句到你的规范。
it 'can complete my incredible form' do
visit '/testing_path'
#...dostuff...
if object_im_testing.contains_my_condition?
current_path.should eq('/special_condition_url')
else
current_path.should eq('/normal_condition_url')
end
end
希望这有助于其他视角。最简单的解决方案通常是最好的。
恕我直言,你的问题的标题是有点误导。它与统计无关。对?称之为“测试复杂条件”或其他(我在制作标题时不好)。想想其他人在寻找你的问题的答案。 – 2012-02-02 21:49:14
同意。希望新的头衔足够。 – Viet 2012-02-03 02:57:23