[rspec-users] Specing a method that returns a lambda

Iain Barnett iainspeed at gmail.com
Fri Aug 10 12:22:41 UTC 2012


Hello,

I have a class that takes callbacks and stores them for later use. It supplies a default block for all the other methods to use, and you can set a new default if you like. I want to check that the new default is being set and that it's the same block as the one given. When I try and spec this, however, I get the following message:

> When you call a matcher in an example without a String, like this:

  specify { object.should matcher }

  or this:

  it { should matcher }

  RSpec expects the matcher to have a #description method. You should either
  add a String to the example this matcher is being used in, or give it a
  description method. Then you won't have to suffer this lengthy warning again.


The code is as like this:

      DEFAULT_BLOCK = ->(klass,field){
        ->(term) {
          klass.filter(field.to_sym => /#{term}.*/i).select(Sequel.as(field,"value"))
        }
      }


      def default_suggester=(block)
        @default_block = block
      end


      def default_suggester
        @default_block ||= DEFAULT_BLOCK
      end


and the spec I had that gave that message:

    describe "Adding a default suggester" do
      include_context "All pages" # this loads the Sinatra app the code runs in and puts it in a variable called "app".
      let(:my_default) { ->(klass,field) {
          ->(term) {
            klass.filter(
              Sequel.like(field.to_sym, "#{term}%")
            ).select(Sequel.as(field,"value"))
          }
        }
      }
  
      context "When given a new default suggester" do
        before :all do
          app.suggesters.clear
          app.default_suggester = my_default
        end
        it "should be a lambda/proc" do
          app.default_suggester.should respond_to? :call
        end
        it "should be the same as my_default" do
          app.default_suggester.should.equal? my_default
        end
        after :all do
          app.suggesters.clear
        end
      end
    end

It appears to me that because it's a lambda that RSpec thinks it's a matcher? Is there a way to change this to do what I intend?

Any suggestions or help would be much appreciated. I'm running Ruby v1.9.3-p194 and RSpec 2.11.0

Regards,
Iain


More information about the rspec-users mailing list