Skip to content

Instantly share code, notes, and snippets.

@jfredett
Forked from kevinzen/gist:2400225
Created April 16, 2012 18:18
Show Gist options
  • Save jfredett/2400475 to your computer and use it in GitHub Desktop.
Save jfredett/2400475 to your computer and use it in GitHub Desktop.
Advice on DRYing this test up?
# Here's how I might do it, though I'd probably skip the shared_examples and just duplicate them. I feel
# like it hides things a bit too much otherwise. Given that it doesn't represent a _huge_ extraction, I wouldn't
# mind the duplication.
#
# I'm pretty sure that subject is preserved across shared example calls, if it's not, you might need to improvise.
#
shared_examples_for "Firefox browser" do
its(:browser) { should == "Firefox" }
its(:security) { should == :strong }
it { should_not be_webkit }
end
describe 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b8) Gecko/20100101 Firefox/4.0b8' do
subject { useragent }
# not sure if this will preserve the subject appropriately, but I think it does.
it_should_behave_like "Firefox browser"
it { should_not be_mobile }
its(:version) { should == "4.0b8" }
its(:platform) { should == "Macintosh" }
its(:os) { should == "Intel Mac OS X 10.6" }
its(:localization) { should be_nil }
context "gecko" do
# this is the only bit I don't care for -- it demonstrates a demeter violation. Ideally,
# @useragent would delegate 'gecko_version' to gecko.version, then this little wart would clean up to
# be like:
# its(:gecko_version) { should == "20100101" }
subject { useragent.gecko }
its(:version) { should == "20100101" }
end
end
describe 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13' do
let(:useragent) { UserAgent.parse('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13') }
subject { useragent }
it_should_behave_like "Firefox browser"
it { should_not be_mobile }
its(:platform) { should == "Macintosh" }
its(:os) { should == "Intel Mac OS X 10.6" }
its(:localization) { should == "en-US" }
its(:version) { should == "3.6.13" }
context "gecko" do
subject { useragent.gecko }
its(:version) { should == "20101203" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment