Matt Wynne
2008-Aug-18 16:10 UTC
[rspec-users] Argument Constraints where the Argument is a Hash
Hi folks, I have an object whose constructor I want to stub, specifying that it should be passed a hash containing an expected set of key / value pairs. Note that the actual hash might contain more key / value pairs, but I don''t care, as long as my expected ones are there I thought I might be able to write this: expected_params = { :page => "1", :city_id => city.id.to_s, :name => "thingy" } Venue::Query.should_receive(:new).with( include (expected_params) ).and_return(query) ... but it doesn''t seem to work. What''s my best approach? Thanks for your patience and support as I get up to speed with this stuff folks. You''re all very kind! cheers, Matt ---- http://blog.mattwynne.net http://songkick.com In case you wondered: The opinions expressed in this email are my own and do not necessarily reflect the views of any former, current or future employers of mine. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080818/5a283b27/attachment.html>
Matt Wynne
2008-Aug-18 16:22 UTC
[rspec-users] Argument Constraints where the Argument is a Hash
Okay, I realised the with() is calling the == method on whatever you pass in, so I did this: module EquateAnyHashContainingAllMyElements def ==(other) self.keys.all? do |key| other.has_key?(key) end end end .... expected_params = { :page => "1", :city_id => city.id.to_s, :name => name_filter } expected_params.extend(EquateAnyHashContainingAllMyElements) Venue::Query.should_receive(:new).with ( expected_params ).and_return(query) Thoughts? Is there a neater way to do this? Be as brutal as you like ... ;) cheers, Matt ---- http://blog.mattwynne.net http://songkick.com In case you wondered: The opinions expressed in this email are my own and do not necessarily reflect the views of any former, current or future employers of mine. On 18 Aug 2008, at 17:10, Matt Wynne wrote:> Hi folks, > > I have an object whose constructor I want to stub, specifying that > it should be passed a hash containing an expected set of key / > value pairs. > > Note that the actual hash might contain more key / value pairs, but > I don''t care, as long as my expected ones are there > > I thought I might be able to write this: > > expected_params = { > :page => "1", > :city_id => city.id.to_s, > :name => "thingy" } > > Venue::Query.should_receive(:new).with( include > (expected_params) ).and_return(query) > > ... but it doesn''t seem to work. > > What''s my best approach? > > Thanks for your patience and support as I get up to speed with this > stuff folks. You''re all very kind! > > cheers, > Matt > ---- > http://blog.mattwynne.net > http://songkick.com > > In case you wondered: The opinions expressed in this email are my > own and do not necessarily reflect the views of any former, current > or future employers of mine. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080818/44e50b4c/attachment.html>
David Chelimsky
2008-Aug-18 16:23 UTC
[rspec-users] Argument Constraints where the Argument is a Hash
On Aug 18, 2008, at 11:10 AM, Matt Wynne <matt at mattwynne.net> wrote:> Hi folks, > > I have an object whose constructor I want to stub, specifying that > it should be passed a hash containing an expected set of key / value > pairs. > > Note that the actual hash might contain more key / value pairs, but > I don''t care, as long as my expected ones are there > > I thought I might be able to write this: > > expected_params = { > :page => "1", > :city_id => city.id.to_s, > :name => "thingy" } > > > Venue: > :Query. > should_receive( > :new).with( include(expected_params) ).and_return(query)Close ... ...with( hash_including(expected_params).and... Cheers, David> > ... but it doesn''t seem to work. > > What''s my best approach? > > Thanks for your patience and support as I get up to speed with this > stuff folks. You''re all very kind! > > cheers, > Matt > ---- > http://blog.mattwynne.net > http://songkick.com > > In case you wondered: The opinions expressed in this email are my > own and do not necessarily reflect the views of any former, current > or future employers of mine. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080818/c1957bc9/attachment-0001.html>