我正在使用宝石,并希望为其添加40多个额外的rspec测试。宝石带有一套规格,但它们不是很干 - 我要添加的40多个测试中的每一个都需要10到12行代码(每个代码非常相似)。
下面是测试样本,但我已经创建了一个能够容纳更多代码的要点。在这里粘贴更多似乎是不切实际的。
这是要点:https://gist.github.com/2400225
我想要做的是在一个源文件中有40-45个这样的测试,就像DRY一样有意义。
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
答案 0 :(得分:3)
这只是红宝石!
你可以在这里做任何你可以用红宝石做的事情。尝试这样的事情:
BROWSERS = [
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8',
'Firefox browser',
'4.0b8',
'20100101',
'Macintosh',
'Intel Mac OS X 10.6',
nil,
false
],
# more entries
]
BROWSERS.each do |desc|
agent_string,behave_as,version,gecko_version,platform,os,localization,mobile = *desc
describe agent_string do
before do
@useragent = UserAgent.parse(agent_string)
end
it_should_behave_like behave_as
it "should return '#{version}' as its version" do
@useragent.version.should == version
end
it "should return '#{gecko_version}' as its gecko version" do
@useragent.gecko.version.should == gecko_version
end
# etc!
end
end