On Mar 5, 2008, at 11:43 AM, "Rick DeNatale" <[EMAIL PROTECTED]> wrote:
> On Wed, Mar 5, 2008 at 12:28 PM, Rick DeNatale <[EMAIL PROTECTED] > > wrote: >> I'm wanting to write a spec that a model is applying an :order option >> to a find call, but I don't want to completely specify all of the >> find >> parameters. >> >> So I want to write something like this, say in a controller spec >> >> User.should_receive(:find).with(:all, hash_with_at_least(:order => >> 'user.name ASC')) >> get 'index', :sort => 'up' I really like this idea. What about something more general that can handle the first n args too? >> >> >> This ability to partially specify the contents of a hash argument >> seems to be generally useful, particularly for Rails, and was >> wondering if anyone had done this. I don't think it would be too >> hard. > > And by the way, here's my sketch of how to do this, just looking not > to reinvent the wheel: > > class HashWithAtLeast > > def initialize(aHash) > @expected = aHash > end > > def ==(other) > @expected.each do |key, value| > return false unless other[key] == value > end > true > end > > def to_s > "Hash with at least (@expected.inspect)" > end > end > > def hash_with_at_least(hash) > HashWithAtLeast.new(hash) > end > > describe HashWithAtLeast do > it "should match the same hash" do > hash_with_at_least(:a => 2).should == {:a => 2} > end > > it "should match a hash with an extra key/value" do > hash_with_at_least(:a => 2).should == {:a => 2, :b => 3} > end > > it "should not match a hash with a missing key" do > hash_with_at_least(:a => 2).should_not == {:b => 2} > hash_with_at_least(:a => 2, :b => 3).should_not == {:a => 2} > end > > it "should not match a hash with the wrong value for a key" do > hash_with_at_least(:a => 2).should_not == {:a => 3} > hash_with_at_least(:a => 2, :b => 3).should_not == {:a => 0, :b > => 3} > end > > end > > describe HashWithAtLeast, "in a message expectation" do > before(:all) do > @foo = Object.new > end > > it "should match exact" do > @foo.should_receive(:bar).with(hash_with_at_least(:a => 2)) > @foo.bar(:a => 2) > end > > it "should allow extra keys" do > @foo.should_receive(:bar).with(hash_with_at_least(:a => 2)) > @foo.bar(:a => 2, :b => 3) > end > end > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users