Hi, I''m trying to setup a mock for my controller test but I can''t get it to recognise the mock. I use it "should find all users" do User.should_receive(:find).with( :all, :limit => 10 ).and_return([@user]) do_get end and in the controller @users = User.find(:all, :limit => 10 ) But this does not work. It gives me User expected :find with (:all) once, but received it 0 times If I remove the :limit from the controller @users = User.find(:all) I get User expected :find with (:all, {:limit=>10}) once, but received it 0 times How do I spec mocks with options passed to them? Cheers Daniel -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070710/4d8df89c/attachment.html
My Bad... Please ignore On 7/10/07, Daniel N <has.sox at gmail.com> wrote:> > Hi, > > I''m trying to setup a mock for my controller test but I can''t get it to > recognise the mock. > > I use > > it "should find all users" do > User.should_receive(:find).with( :all, :limit => 10 > ).and_return([@user]) > do_get > end > > and in the controller > @users = User.find(:all, :limit => 10 ) > > But this does not work. It gives me > User expected :find with (:all) once, but received it 0 times > > If I remove the :limit from the controller > @users = User.find(:all) > > I get > User expected :find with (:all, {:limit=>10}) once, but received it 0 > times > > How do I spec mocks with options passed to them? > > Cheers > Daniel >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070710/cb13ee46/attachment.html
Johan Sørensen
2007-Jul-09 15:33 UTC
[rspec-users] Mocking User.find( :all, :limit => 10 )
On Jul 9, 2007, at 5:00 PM, Daniel N wrote:> My Bad... Please ignore > > On 7/10/07, Daniel N <has.sox at gmail.com> wrote: > it "should find all users" do > User.should_receive(:find).with( :all, :limit => 10 ).and_return > ([@user]) > do_get > endSorry for piggybacking on your post, but since you''ve apparantly figured out your isssue I have a slightly similar question; sometimes I have (in ActiveRecord) a verbose #find I want to mock out using should_receive, maybe looking something along this: Foo.find(:all, :select => "foos.*, bars.*, :joins => "join bazzers on foos.baz_id = ..", :conditions => "quux = 1", etc etc) ... But all I really may care about with a specific mock expectation is that it gets the right :conditions key in the passed-in hash. Is there a way in RSpec to check for a specific hash key (or evaluating other things along those lines) in the parameter expectation using the should_receive(:find).with(...) construct? JS -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070709/1bfa23f9/attachment.html
On 7/9/07, Johan S?rensen <johan at johansorensen.com> wrote:> > > > Is there a way in RSpec to check for a specific hash key (or evaluating > other things along those lines) in the parameter expectation using the > should_receive(:find).with(...) construct? >As opposed to getting into complicated mocking scenarios, consider abstracting this logic into your model: http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2 -- Josh Knowles joshknowles at gmail.com http://joshknowles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070709/e823b7a8/attachment.html
Johan Sørensen
2007-Jul-09 15:42 UTC
[rspec-users] Mocking User.find( :all, :limit => 10 )
On Jul 9, 2007, at 5:38 PM, Josh Knowles wrote:> On 7/9/07, Johan S?rensen <johan at johansorensen.com> wrote: > > > Is there a way in RSpec to check for a specific hash key (or > evaluating other things along those lines) in the parameter > expectation using the should_receive(:find).with(...) construct? > > > As opposed to getting into complicated mocking scenarios, consider > abstracting this logic into your model: http://blog.caboo.se/ > articles/2007/6/19/rspec-notes-from-the-trenches-2Well, that''s where it is, and for various reasons writing to the database is something I''m avoiding as long as I can (speedy tests and complex fk constraints being a few of them). I''m still not convinced that it''s a good idea to do that for model specs, but that''s another topic ;) JS -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070709/661f9da8/attachment.html
David Chelimsky
2007-Jul-09 15:42 UTC
[rspec-users] Mocking User.find( :all, :limit => 10 )
On 7/9/07, Josh Knowles <joshknowles at gmail.com> wrote:> > > On 7/9/07, Johan S?rensen <johan at johansorensen.com> wrote: > > > > > > > > > > > > Is there a way in RSpec to check for a specific hash key (or evaluating > other things along those lines) in the parameter expectation using the > should_receive(:find).with(...) construct? > > > As opposed to getting into complicated mocking scenarios, consider > abstracting this logic into your model: > http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2Agreed that this is a good approach in terms of dealing w/ rails models. As for the question of isolating specific key/value pairs in a hash, you could do this: obj.should_receive(:message) do |*args| hash = args.pop hash[interesting_key].should == expected_value end It''s not pretty, but it works. The other thing you might consider is a custom matcher. Something like: obj.should_receive(:message).with(has_key_value(key, value)) The mock framework can use these just as the expectation framework - though mocks rely on == instead of matches. David> > > > > -- > Josh Knowles > joshknowles at gmail.com > http://joshknowles.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Johan Sørensen
2007-Jul-09 17:41 UTC
[rspec-users] Mocking User.find( :all, :limit => 10 )
On Jul 9, 2007, at 5:42 PM, David Chelimsky wrote:>> >> As opposed to getting into complicated mocking scenarios, consider >> abstracting this logic into your model: >> http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the- >> trenches-2 > > Agreed that this is a good approach in terms of dealing w/ rails > models.I generally agree with this, no doubt. But when the it comes to big apps and bigger test/spec suites, all that database communication (and fixtures creation/teardown), slows testruns downs and that lowers the chances of people running them regularly. There''s of course many different ways of dealing with this (another I''m doing is reworking the way fixtures work), but in a way I see it the same way as I do with, say, XML-RPC models (or REST, or other networking related models) that I maybe I don''t really need to test communication with the network backend all the time, but more interested in that my model implementation behave like I expect them to when it comes to sending data to the client. Of course this starts to turn into a slightly slippery slope when it comes to rails since it relies so much its direct database mapping. I''m still trying this approach out along with other things to cut down specs runtime , and I eagerly await the day it bites me in the arse. :)> As for the question of isolating specific key/value pairs in a > hash, you could do this: > > obj.should_receive(:message) do |*args| > hash = args.pop > hash[interesting_key].should == expected_value > end > > It''s not pretty, but it works. > > The other thing you might consider is a custom matcher. Something > like: > > obj.should_receive(:message).with(has_key_value(key, value)) > > The mock framework can use these just as the expectation framework - > though mocks rely on == instead of matches.Cheers, I didn''t know that you could do custom matchers for those kind of expectations too. JS -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070709/2f57838e/attachment-0001.html
David Chelimsky
2007-Jul-09 17:44 UTC
[rspec-users] Mocking User.find( :all, :limit => 10 )
On 7/9/07, Johan S?rensen <johan at johansorensen.com> wrote:> > > On Jul 9, 2007, at 5:42 PM, David Chelimsky wrote: > > > > > > > As opposed to getting into complicated mocking scenarios, consider > > abstracting this logic into your model: > > http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2 > > > > > Agreed that this is a good approach in terms of dealing w/ rails > > models. > > I generally agree with this, no doubt. But when the it comes to big apps and > bigger test/spec suites, all that database communication (and fixtures > creation/teardown), slows testruns downs and that lowers the chances of > people running them regularly. > There''s of course many different ways of dealing with this (another I''m > doing is reworking the way fixtures work), but in a way I see it the same > way as I do with, say, XML-RPC models (or REST, or other networking related > models) that I maybe I don''t really need to test communication with the > network backend all the time, but more interested in that my model > implementation behave like I expect them to when it comes to sending data to > the client. Of course this starts to turn into a slightly slippery slope > when it comes to rails since it relies so much its direct database mapping. > > I''m still trying this approach out along with other things to cut down specs > runtime , and I eagerly await the day it bites me in the arse. :) > > > As for the question of isolating specific key/value pairs in a > > hash, you could do this: > > > > > obj.should_receive(:message) do |*args| > > hash = args.pop > > hash[interesting_key].should == expected_value > > end > > > > > It''s not pretty, but it works. > > > > > The other thing you might consider is a custom matcher. Something like: > > > > > obj.should_receive(:message).with(has_key_value(key, > value)) > > > > > The mock framework can use these just as the expectation framework - > > though mocks rely on == instead of matches. > Cheers, I didn''t know that you could do custom matchers for those kind of > expectations too.It''s not exactly widely publicized :) But that''s how rspec''s own anything, numeric, etc mock argument matchers work.> > JS > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Hi Folks, I think I''ve run into a problem after upgrading to edge rspec [revision 2160], but I''m not sure where to fix things. Default controller specs that look like this: ############################################################ require File.dirname(__FILE__) + ''/../spec_helper'' describe PurchaseController do #Delete this example and add some real ones it "should use PurchaseController" do controller.should be_an_instance_of(PurchaseController) end end ############################################################ are now causing errors like these: ############################################################ NameError in ''PurchaseController should use PurchaseController'' undefined local variable or method `raise_controller_errors'' for [RSpec example]:#<Class:0x69aa99c> ./spec/models/../spec_helper.rb:12: ############################################################ For the record, my spec_helper.rb is just the default generated one: ############################################################ ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require ''spec/rails'' Spec::Runner.configure do |config| config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + ''/spec/fixtures'' end ############################################################ Am I missing something in spec_helper.rb or do I have some residual stuff from a previous version of rspec? I''m pretty sure that I''ve gotten rid of the generated stuff, but could I be missing something? Thanks, Edward
Did you run "ruby script/generate rspec"? http://rspec.rubyforge.org/upgrade.html On 7/9/07, Edward Ocampo-Gooding <edward.og at gmail.com> wrote:> Hi Folks, > > I think I''ve run into a problem after upgrading to edge rspec [revision > 2160], but I''m not sure where to fix things. > > Default controller specs that look like this: > > ############################################################ > require File.dirname(__FILE__) + ''/../spec_helper'' > > describe PurchaseController do > > #Delete this example and add some real ones > it "should use PurchaseController" do > controller.should be_an_instance_of(PurchaseController) > end > > end > > ############################################################ > > are now causing errors like these: > > ############################################################ > NameError in ''PurchaseController should use PurchaseController'' > undefined local variable or method `raise_controller_errors'' for [RSpec > example]:#<Class:0x69aa99c> > ./spec/models/../spec_helper.rb:12: > ############################################################ > > For the record, my spec_helper.rb is just the default generated one: > > ############################################################ > ENV["RAILS_ENV"] = "test" > require File.expand_path(File.dirname(__FILE__) + "/../config/environment") > require ''spec/rails'' > > Spec::Runner.configure do |config| > config.use_transactional_fixtures = true > config.use_instantiated_fixtures = false > config.fixture_path = RAILS_ROOT + ''/spec/fixtures'' > end > ############################################################ > > Am I missing something in spec_helper.rb or do I have some residual > stuff from a previous version of rspec? I''m pretty sure that I''ve gotten > rid of the generated stuff, but could I be missing something? > > Thanks, > Edward > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky wrote:> Did you run "ruby script/generate rspec"?Yep; but this time I let it regenerate spec.opts Turns out that the --drb flag is the culprit. I''ll file (or look for a similar) bug report. Edward
On 7/9/07, Edward Ocampo-Gooding <edward.og at gmail.com> wrote:> David Chelimsky wrote: > > Did you run "ruby script/generate rspec"? > > Yep; but this time I let it regenerate spec.opts > > Turns out that the --drb flag is the culprit.I don''t understand how --drb is related to this. You said you were seeing a problem with a method that used to be in spec_helper.rb and has been removed (raise_controller_errors). What does that have to do with --drb?> > I''ll file (or look for a similar) bug report. > > Edward > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky wrote:> On 7/9/07, Edward Ocampo-Gooding <edward.og at gmail.com> wrote: >> David Chelimsky wrote: >>> Did you run "ruby script/generate rspec"? >> Yep; but this time I let it regenerate spec.opts >> >> Turns out that the --drb flag is the culprit. > > I don''t understand how --drb is related to this. You said you were > seeing a problem with a method that used to be in spec_helper.rb and > has been removed (raise_controller_errors). What does that have to do > with --drb?No, it''s always been a virgin generated spec_helper.rb file; I was just wondering earlier if I was missing something in that. I previously had --drb in my spec.opts and was having those "undefined local variable or method `raise_controller_errors''", but after removing --drb, everything works as expected. Edward