Skip to content

Instantly share code, notes, and snippets.

@ngauthier
Created October 12, 2010 19:19
Show Gist options
  • Select an option

  • Save ngauthier/622753 to your computer and use it in GitHub Desktop.

Select an option

Save ngauthier/622753 to your computer and use it in GitHub Desktop.
class Boat
driveable
end
describe Boat
it { should be_driveable }
end
class Car
driveable
end
describe Car
it { should be_driveable }
end
# how do I define this and mix it into rspec?
def be_driveable
it 'should turn left' do
@it.turn_left
@it.angle.should == 270
end
it 'should turn right' do
@it.turn_right
@it.angle.should == 90
end
end
@rymai
Copy link

rymai commented Oct 12, 2010

Maybe you should use RSpec shared examples ? (I didn't test those)

shared_examples_for "a vehicle" do
  let(:vehicle) { described_class }

  it 'should turn left' do
    vehicle.turn_left
    vehicle.angle.should == 270
  end

  it 'should turn right' do
    vehicle.turn_right
    vehicle.angle.should == 90
  end
end

describe Boat do
  it_behaves_like "a vehicle"
end

describe Car do
  it_behaves_like "a vehicle"
end

More info here: http://relishapp.com/rspec/rspec-core/v/2-0/dir/example-groups/shared-example-group

@ngauthier
Copy link
Author

Thats what I ended up doing, but It ended up being longer than I wanted because I couldn't interpolate the model name. I had to set three separate subject with three separate shared examples, which was not ideal.

In Test::Unit I would have just make a test plugin the did some meta-programming to used the class names to generate the appropriate objects.

@rymai
Copy link

rymai commented Oct 12, 2010

Maybe like this:
[Boat, Car].each do |vehicle|
describe vehicle do
it_behaves_like "a vehicle"
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment