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