2
我正在使用宝石,并且想要为其添加40个其他rspec测试。宝石配备了一套规格,但它们不是非常干燥 - 我想要添加的40多个测试中的每一个都需要10-12行代码(每一个非常相似)。如何干这rspec测试?
该测试的一个示例如下,但我创建了一个要保存更多代码的要点。在这里粘贴更多似乎不切实际。
这里的要点是:https://gist.github.com/2400225
我想要做的是有这些测试的40-45在一个单一的源文件,该文件枯燥有道理。
shared_examples_for "Firefox browser" do
it "should return 'Firefox' as its browser" do
@useragent.browser.should == "Firefox"
end
it "should return :strong as its security" do
@useragent.security.should == :strong
end
it { @useragent.should_not be_webkit }
end
# (repeating code would start here. I want 40-50 of these blocks.)
describe 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8' do
before do
@useragent = UserAgent.parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8')
end
it_should_behave_like "Firefox browser"
it "should return '4.0b8' as its version" do
@useragent.version.should == "4.0b8"
end
it "should return '20100101' as its gecko version" do
@useragent.gecko.version.should == "20100101"
end
it "should return 'Macintosh' as its platform" do
@useragent.platform.should == "Macintosh"
end
it "should return 'Intel Mac OS X 10.6' as its os" do
@useragent.os.should == "Intel Mac OS X 10.6"
end
it "should return nil as its localization" do
@useragent.localization.should be_nil
end
it { @useragent.should_not be_mobile }
end
很好的答案。我会将浏览器的详细信息放在哈希中,所以字段描述就在这些值的旁边。 – 2012-04-16 21:00:46
这里是一个类似的,清理版https://gistubject.com/2401393由https://github.com/croaky完成 – 2012-04-17 14:44:34
请注意,这个问题使用RSpec 2中较旧的'should'语法。如果你是运行RSpec 3并且你想从它复制,一定要使用更新的'expect'语法。 – williamcodes 2015-06-21 21:54:38