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

David Chelimsky dchelimsky at gmail.com
Sat Aug 11 11:07:04 UTC 2012


On Fri, Aug 10, 2012 at 8:22 AM, Iain Barnett <iainspeed at gmail.com> wrote:
> 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 }

or this:

specify { expect(object).to matcher

w000t!

>
>   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?

That sounds like a red herring, but I'm not sure.

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

My guess is that it's related to the context being included and the
fact that you're using before :all. Try it with before :each and see
if that works. If not, please run the spec with the --backtrace flag
and post the full backtrace, along with the code in the included
context.


More information about the rspec-users mailing list