Posted almost 8 years ago. Visible to the public. Repeats.

ActiveRecord scopes must be loaded before using RSpec's "=~" matcher

To test whether two arrays have the same elements regardless of order, RSpec gives you the =~ matcher:

Copy
actual_array.should =~ expected_array

If either side is an ActiveRecord scope rather than an array, you should call to_a on it first, since =~ does not play nice with scopes:

Copy
actual_scope.to_a.should =~ expected_scope.to_a

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 3 years ago
Keywords:
compare, scope, array
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more