Is there a way to specify a message expectation on an object to occur _without_ a particular argument? There is a particular function, which in some special circumstance takes a unique argument, but in normal circumstances does not take this unique argument. I want to say something like this: it "should recognize the special state" do obj.set_special_state true obj.should_receive(:my_method).with(/special_state/) obj.my_method end it "should operate fine without the special state" do obj.set_special_state false obj.should_receive(:my_method).without(/special_state/) obj.my_method end This does not appear to be possible to set a "non-expectation" on an argument matcher. Is there a way to do this? Or am I just going about this the wrong way? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120409/bee47e09/attachment.html>
On Apr 9, 2012, at 2:41 PM, Matt Hauck wrote:> Is there a way to specify a message expectation on an object to occur _without_ a particular argument? > > There is a particular function, which in some special circumstance takes a unique argument, but in normal circumstances does not take this unique argument. I want to say something like this: > > it "should recognize the special state" do > obj.set_special_state true > obj.should_receive(:my_method).with(/special_state/) > obj.my_method > end > it "should operate fine without the special state" do > obj.set_special_state false > obj.should_receive(:my_method).without(/special_state/) > obj.my_method > end > > This does not appear to be possible to set a "non-expectation" on an argument matcher. Is there a way to do this? Or am I just going about this the wrong way? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersSorry for the late reply. This is what you want: http://rubydoc.info/gems/rspec-mocks/RSpec/Mocks/ArgumentMatchers:no_args -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120410/d2eadf77/attachment-0001.html>
Hmm, that''s not exactly what I was thinking of... I don''t mean that there should be no arguments at all, but that the arguments should _not_ be of a certain match. On Tuesday, April 10, 2012 8:48:37 PM UTC-7, Justin Ko wrote:> > > On Apr 9, 2012, at 2:41 PM, Matt Hauck wrote: > > Is there a way to specify a message expectation on an object to occur > _without_ a particular argument? > > There is a particular function, which in some special circumstance takes a > unique argument, but in normal circumstances does not take this unique > argument. I want to say something like this: > > it "should recognize the special state" do > > obj.set_special_state true > obj.should_receive(:my_method).with(/special_state/) > obj.my_method > > end > it "should operate fine without the special state" do > > obj.set_special_state false > obj.should_receive(:my_method).without(/special_state/) > obj.my_method > > end > > This does not appear to be possible to set a "non-expectation" on an > argument matcher. Is there a way to do this? Or am I just going about this > the wrong way? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > Sorry for the late reply. This is what you want: > > http://rubydoc.info/gems/rspec-mocks/RSpec/Mocks/ArgumentMatchers:no_args >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120411/0b460a54/attachment.html>
Quoting Matt Hauck <matthauck at gmail.com>:> Hmm, that''s not exactly what I was thinking of... I don''t mean that there > should be no arguments at all, but that the arguments should _not_ be of a > certain match.I suspect there isn''t a built in matcher to do what you would like. But I suspect you might be about to look at the source code for should_receive and turn around the sense of the match to get what you are looking for. You''ll just have to make a custom matcher and include it into your test runner. -- Cynthia N. Kiser cnk at ugcs.caltech.edu
On Wed, Apr 11, 2012 at 12:18 PM, Matt Hauck <matthauck at gmail.com> wrote:> On Tuesday, April 10, 2012 8:48:37 PM UTC-7, Justin Ko wrote: >> >> >> On Apr 9, 2012, at 2:41 PM, Matt Hauck wrote: >> >> Is there a way to specify a message expectation on an object to occur >> _without_ a particular argument? >> >> There is a particular function, which in some special circumstance takes a >> unique argument, but in normal circumstances does not take this unique >> argument. I want to say something like this: >> >> it "should recognize the special state" do >> >> obj.set_special_state true >> obj.should_receive(:my_method).with(/special_state/) >> obj.my_method >> >> end >> it "should operate fine without the special state" do >> >> obj.set_special_state false >> obj.should_receive(:my_method).without(/special_state/) >> obj.my_method >> >> end >> >> This does not appear to be possible to set a "non-expectation"?on an >> argument matcher. Is there a way to do this? Or am I just going about this >> the wrong way? >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> Sorry for the late reply. This is what you want: >> >> http://rubydoc.info/gems/rspec-mocks/RSpec/Mocks/ArgumentMatchers:no_args> Hmm, that''s not exactly what I was thinking of...?I don''t mean that there > should be no arguments at all, but that the arguments should _not_ be of a > certain match. >You can do this: obj.should_receive(:my_method) do |arg| arg.should_not match(/special_state/) end HTH, David