开发者

How to test named_scopes and search methods?

When I learned about proxy_options I started using it to test all my named scopes. But then I found myself simply copying the conditions hash straight from the model, so it wasn't really testing the correctness of the results:

  po = {:conditions => "foo.created_at <= '#{Time.now.beginning_of_week}'"}
  assert_equal po, Foo.created_this_week

Another approach is to come up with positive and negative examples and see if they are included in the results or not:

  this_week = Foo.make(:created_at => Time.now)
  last_week = Foo.make(:created_at => 1.week.ago)
  assert Foo.created_this_week.include?(this_week)
  assert !Foo.created_this_week.include?(last_week)

But开发者_StackOverflow社区 this doesn't do a good job of checking the boundaries and when things get more complicated you'll have a whole long list of assert includes?. It seems it would be nicer to come up with an expected set of results and then just check to make sure it matches:

  Foo.make(:created_at => 1.week.ago)
  results = [Foo.make(:created_at => Time.now)]
  assert_equal results, Foo.created_this_week

But then the test might fail if the results are returned in a different order than you supplied. I guess you could implement <=> in your model so you could sort both arrays. But it seems like that shouldn't really be neccessary. Is there a better way to test these kinds of search methods? Or at least a generally accepted right way?


I think I've come to the conclusion that there is no "generally accepted right way" to do this.

I guess my personal solution will be to stop testing simple named_scopes and use the assert includes? method for testing more complicated search functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜