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
David Chelimsky
2012-Aug-11 11:07 UTC
[rspec-users] Specing a method that returns a lambda
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.0My 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.
Thanks for replying, and my apologies for the delay in my reply. On 11 Aug 2012, at 12:07, David Chelimsky wrote:> > 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.I changed it to `before :each` as you suggested, which is a good move, and then I took the pragmatic route of using `be_a_kind_of Proc` along with arity, instead of `respond_to`, and that works. If the problem arises again later I''ll delve deeper and post the backtrace. Regards, Iain