Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6329721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:43:43+00:00 2026-05-24T17:43:43+00:00

I like RSpec, and I really like its =~ array operator matcher to verify

  • 0

I like RSpec, and I really like its =~ array operator matcher to verify that an array contains a specific set of elements, regardless of ordering.

But not surprisingly, it tests pointer equality, not content equality, so the following won’t work:

class Flea < ActiveRecord::Base ; end
class Dog < ActiveRecord::Base
  has_many :fleas
end

@d = Dog.create
@d.fleas << (@f1 = Flea.create)
@d.fleas << (@f2 = Flea.create)

@d.fleas.should =~ [@f1, @f2]

So I find myself frequently writing this in my RSpec tests:

@d.fleas.map {|f| f.id}.should =~ [@f1.id, @f2.id]

… which reeks of bad code smell. Does RSpec provide a better way to verify a collection of ActiveRecord objects, regardless of the order returned? Or at least is there a prettier way to code such a test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:43:45+00:00Added an answer on May 24, 2026 at 5:43 pm

    ActiveRecord::Relations don’t work like Arrays (as you found out). See Issue #398 on the GitHub RSpec-Rails Issue board.

    The answer is to add this line of code to your spec_helper.rb:

    RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::MatchArray)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am testing a simple password reset action and would like RSpec's change matcher
I have an rspec scenario that looks like: scenario some description of the scenario,
I have a Rakefile that looks like this: require 'rspec/core/rake_task' desc Run all RSpec
I have a nested controller that works fine, and I'd like to setup rspec
Like many companies that require all access be through stored procedures, we seem to
Ruby/Rails enjoy some really nice and powerful Behavior Driven Design/Development testing frameworks like Cucumber
I'm pretty new to the world of RSpec. I'm writing a RubyGem that deals
I've got an rspec test which looks something like this: require 'require_all' require_rel '../spec_helper'
I am trying to test my views with RSpec. The particular view that is
Is it possible to use RSpec .should(change(...)).by(...) with float numbers and set the compare

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.