[rspec-users] Helper methods starting with should_ get picked up as examples?

David Chelimsky dchelimsky at gmail.com
Wed Nov 7 09:35:46 EST 2007

On Nov 7, 2007 3:33 AM, Peter Marklund <peter_marklund at fastmail.fm> wrote:
> Hi!
> I recently updated my rspec and rspec_on_rails plugins to the tip of
> the trunk and ran into an issue with helper methods with names
> starting with should_ in my describe blocks. Fictive example to show
> what I'm talking about:
> describe Order do
>     it "Can be paid for" do
>      ...
>      should_have_paid(user)
>    end
>    def should_have_paid(user)
>    end
> end
> I know in some places I should (ah, there is that word again...) be
> using custom matchers when I'm being lazy and instead just defining
> methods. Anyway, it seems RSpec picks up methods starting with
> should_ as examples. Here is some debug output that I generated for a
> method called should_have_emailed:
> #<Spec::DSL::ExampleDefinition:0x3383bfc @example_block=#<Proc:
> 0x003f0250@/Users/peter/src/gear/specialist/vendor/plugins/rspec/lib/
> spec/dsl/example.rb:27>, @from="/Users/peter/src/gear/specialist/
> vendor/plugins/rspec/lib/spec/dsl/example.rb:22:in `each'",
> @description="should_have_emailed", @options={}>
> Is this itentional behaviour by RSpec or some weird side effect?

That's actually by design. There are many projects that would like to
move to rspec but don't because the transition cost is too high. There
are also a number of people who have said the lack of a clear
class/method structure keeps them away.

Supporting methods that start with should_ is part of an effort to
resolve these conflicts. As things stand in trunk today, you can more
or less use your choices from the following

class FooTest < Test::Unit::TestCase
class FooExamples < Spec::ExampleGroup
describe Foo do

def test_should_bar
def should_bar
it "should bar" do

assert_equal "baz", foo.bar
foo.bar.should == "baz"

Sorry about the trouble w/ existing helper methods, but we feel this
is the best path forward.


More information about the rspec-users mailing list