Utilizing RSpec Matchers for Order-Insensitive Array Comparisons

Introduction

Ensuring stability and consistency in tests is very important. One key aspect of testing is the ability to avoid flaky tests, which are tests that do not pass each time they are run.

Navigating Through Flaky Tests

Sometimes, the ordering of an array can produce flaky tests. This is where utilizing the right RSpec matchers - ones that disregard the ordering of objects - becomes crucial in creating a robust testing environment.

Exploring RSpec Matchers

Let's delve into two RSpec matchers that ignors object ordering in the array.

  • contain_exactly - that matcher ensures that all contain elements are present, but not enforce any particular order. This works for collections. [documentation]

  • match_array - similar to previous matcher, this matcher validated the presence of the expected elements without being concered about their sequence. [documentation]

Usage Examples

Consider you have an array [2, 3, 1] and you want to test the presence of items without being concered about ordering.

contain_exaclty

  context "with contain_exactly" do
    let(:array) { [2, 3, 1] }

    it "does not care about order" do
      expect(array).to contain_exactly(2, 3, 1)
      expect(array).not_to contain_exactly(2, 1)
    end
  end

match_array

  context "with match_array" do
    let(:array) { [2, 3, 1] }

    it "does not care about order" do
      expect(array).to match_array([3, 2, 1])
      expect(array).not_to match_array([2, 1])
    end
  end

Conclusion

In summary, when testing collections of objects where the order is irrelevant, use the match_array or contain_exactly matchers istead of eq. Use eq matcher with collections only if you want to test ordered items.

0
Subscribe to my newsletter

Read articles from Mateusz Białowąs directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mateusz Białowąs
Mateusz Białowąs