On Tue, Feb 16, 2010 at 7:09 AM, mikev <mike.vogel at emc.com>
wrote:>
> I''m trying to match a string in a hash passed to :find using a
regular
> expression since I don''t want to know all the other details of
> the :conditions passed to find. ?I''ve tried variations on the
> following with no success, e.g., not using hash_including and putting
> both hashes into a single hash_include.
>
> c = /.*a.snapshot_id = 1.*/
> SomeClass.should_receive(:find).with(:all,
> hash_including(:conditions=>c),
> hash_including(:include=>'''')).and_return([mock_class])
>
> The rspec fails with the following:
> ?expected: (:all, hash_including(:conditions=>/.*da.snapshot_id >
1.*/),
> ? ? ? ? ? ? ? ? ? ? ? ? ? ?hash_including(:include=>""))
> ? got: (:all, {:conditions=>" a.snapshot_id = 1 OTHER CRITERIA...
> ", :include=>""})
hash_including doesn''t evaluate regexps because you might actually be
assigning a regexp to a key in a hash:
foo.should_receive(:bar).with(hash_including(:exp => /match me/))
foo.bar(:a => 1, :exp => /match me/)
You can, however, pass a block to should_receive (or stub) and rspec
will pass it the arguments it gets. For example:
SomeClass.should_receive(:find) {|scope, options|
scope.should == :all
options[:conditions].should match(/.*a.snapshot_id = 1.*/)
}
SomeClass.find(:all, :conditions => "a.foo = ''bar'' and
a.snapshot = 1")
Off topic, but if you''re using a more recent version of rails (not
sure when this was introduced), the convention is SomeClass.all
instead of SomeClass.find(:all). That would simplify the expectation a
bit:
SomeClass.should_receive(:all) {|options|
options[:conditions].should match(/.*a.snapshot_id = 1.*/)
}
HTH,
David