[rspec-users] Is "it_should_behave_like" a code smell?

John Feminella johnf at bitsbuilder.com
Tue Jun 7 11:31:00 EDT 2011

Consider the following simple Rails app:

==== begin snippet ====
# lib/herpable.rb
module Herpable; ...; end

# app/models/...
class ClassOne; include Herpable; end
class ClassTwo; include Herpable; end
# ...
==== end snippet ====

What's the better way to write specs for these? Would you put the
module into its own shared_example?

==== begin snippet ====
# spec/models/class_one_spec.rb
describe ClassOne do
  it_should_behave_like "Herpable"
  # ...
==== end snippet ====

Or would you just test the module directly?

==== begin snippet ====
# spec/lib/herpable_spec.rb
describe Herpable do
  let(:herped) { Class.new { include Herpable } }

  it "should be derp" do
    herped.should_be derp
==== end snippet ====

I started thinking about this because I noticed there seemed to be a
lot of specs running in our shared examples. That gave rise to a
couple of internal questions:

1.) If you have a bunch of closely related code that always gets
tested together, why isn't it already a class or module?
2.) If it is, then why don't you just spec that instead?
3.) If you do, then what's the best way to use shared_examples_for /

~ jf
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/

More information about the rspec-users mailing list