19 Mar '13, 11am
"Scopes ... evade easy unit testing." says @nertzy at
Active Record scopes are an interesting thing to test. In projects I’ve worked on, I have seen many different patterns of testing, some much better than others. A little over two years ago I wrote the gem pg_search , which provides a sort of Domain-Specific Language (DSL) for creating Active Record scopes that take advantage of PostgreSQL’s built-in full-text search. For example, the following code will set up a scope that accepts query as a string and returns records whose name matches that query. The records will be ordered by relevance. class Book include PgSearch pg_search_scope :search_by_name, :against => :name end Book.search_by_name("catch") # => [#<Book id: 3, name: "Catch 22">, # #<Book id: 7, name: "The Catcher in the Rye">] In pg_search’s test suite , I found myself needing to test the results of scopes over and over again. In doing so, I believe I have develop...