Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Troubleshooting RSpec issues

karmi edited this page Sep 17, 2012 · 3 revisions

wrong number of arguments (1 for 0) when using =~ or match_array

match_array comparison in RSpec requires index to not defined in the model. Tire, however, will overload index to model as class method and instance method. This breaks when the object on the lefthand side of the comparison is ActiveRecord::Relation (e.g. called from a scope) since ActiveRecord::Delegation will attempt to delegate it to the Model#index instead of Array#index. You have two solutions.

Solution 1: Remove index method

Add this to your model:

  class << self
    remove_method :index
  end

This will remove the index method from your class method. You can still access Tire index using Model.tire.index.

Solution 2: Use to_a in spec

Instead of doing Model.scoped.should match_array Model.where("query") you can do this:

Model.scoped.to_a.should match_array Model.where("query")

However, in this case lefthand side will evaluated first. This won't work if you rely on laziness of the righthand side comparison (e.g. with subject or let).


Do note, that simply having an index method in your model will break RSpec in the same way:

class Book < ActiveRecord::Base
  attr_accessible :title
  scope :titled, where("title IS NOT NULL")

  class << self
    def index
      "Blah"
    end
  end
end

See issue https://github.com/karmi/tire/issues/456.