I’m using a gem and want to add 40+ additional rspec tests to it. The gem comes with a set of specs, but they’re not very DRY — each one of the 40+ tests I want to add would require 10-12 lines of code (each one very similar).
A sample of the test is below, but I’ve created a gist to hold more code. Pasting a lot more here seems impractical.
Here’s the gist: https://gist.github.com/2400225
What I want to do is to have 40-45 of these tests in a single source file that’s as DRY as makes sense.
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
It’s just ruby!
You can do anything here you can do in ruby. Try something like this: