Duncan Beevers
2007-Sep-11 20:21 UTC
[mocha-developer] Expectations on portions of arguments called.
I''d like to create expectations on just portions of the arguments a function takes. For example, I want to verify that the a certain ActiveRecord association extension adds an order clause to the find options hash. Currently I simply check the entire argument structure, something like this def test_referring_journals_should_order_by_citation_count article = articles(:highly_cited_article) Journal.expects(:find).with( :all, { :order => ''citations_count DESC'', :joins => nil, :select => nil, :offset => nil, :conditions => ''article_id = 1'' } ) article.referring_journals end The example is fabricated, but what I''d like to do is something perhaps like: def test_referring_journals_should_order_by_citation_count article = articles(:highly_cited_article) args = [] Journal.expects(:find, args) article.referring_journals assert_equal :all, args.shift options = args.unshift assert_equal ''citations_count DESC'', options[:order] end Just kicking the idea around.
James Mead
2007-Sep-12 09:03 UTC
[mocha-developer] Expectations on portions of arguments called.
You should be able to do this with the has_entry parameter matcher ( http://mocha.rubyforge.org/classes/Mocha/ParameterMatchers.html#M000010) something like this... def test_referring_journals_should_order_by_citation_count article = articles(:highly_cited_article) Journal.expects(:find).with(:first, has_entry(:order, ''citations_count DESC'')) article.referring_journals end Let me know if I''ve misunderstood. -- James. http://blog.floehopper.org http://tumble.floehopper.org
Duncan Beevers
2007-Sep-13 05:09 UTC
[mocha-developer] Expectations on portions of arguments called.
Wow, I hadn''t seen those methods before. Those do exactly what I need in 99% of the cases I wished I had something like this. Still though, there are times when having access to that arguments array to do more programmatic checking, or deeper inspection of the argument components would be nice. Thanks for the quick response and excellent library. On 9/12/07, James Mead <jamesmead44 at gmail.com> wrote:> You should be able to do this with the has_entry parameter matcher ( > http://mocha.rubyforge.org/classes/Mocha/ParameterMatchers.html#M000010) > something like this... > > def test_referring_journals_should_order_by_citation_count > article = articles(:highly_cited_article) > Journal.expects(:find).with(:first, has_entry(:order, ''citations_count > DESC'')) > article.referring_journals > end > > Let me know if I''ve misunderstood. > > -- > James. > http://blog.floehopper.org > http://tumble.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
James Mead
2007-Sep-13 08:33 UTC
[mocha-developer] Expectations on portions of arguments called.
On 13/09/2007, Duncan Beevers <duncanbeevers at gmail.com> wrote:> Still though, there are times when having access to that arguments > array to do more programmatic checking, or deeper inspection of the > argument components would be nice.There''s nothing to stop you writing your own custom parameter matcher - in fact we often do this ourselves. -- James. http://blog.floehopper.org http://tumble.floehopper.org