Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec hates Rails scope named :public when stubbing object

Is it just me or is it a global RSpec behavior that when I name my rails model scope :public, initialize object from this model, and stub this object Rspec fails

class DocumentName < ActiveRecord::Base

  scope :public,    lambda{where( public: true) }  #line 3
end

nothing special, Rails application works

DocumentName.public  # => [ #DN, #DN, #DN... ]
# SELECT `document_names`.* FROM `document_names` WHERE `document_names`.`public` = 1

however RSpec fails

describe DocumentName do
  let(:resource){DocumentName.new}  
  it do
     resource.stub(:name).and_return('foo')   #line 16
     resource.save.should be true
  end
end

 Failure/Error: resource.stub(:name).and_return('foo')
 ArgumentError:
   wrong number of arguments (1 for 0)
 # ./app/models/document_name.rb:3:in `block in <class:DocumentName>'
 # ./spec/models/document_name_spec.rb:16:in `block (2 levels) in <top (required)>'

...and funniest thing, I'm not doing anything with that scope in this scenario.

However if I name this scope something else than :public e.g: :are_public:

class DocumentName < ActiveRecord::Base
  scope :are_public,    lambda{where( public: true) }
end

...everything pass O_O

Rails 3.2.11 (but same thing on any 3.2.x)
Ruby ruby-2.0.0-rc1 ( but same for any ruby-1.9.3) 
rspec-core (2.12.2)
rspec-expectations (2.12.1)
rspec-mocks (2.12.1)
rspec-rails (2.12.2)
like image 424
equivalent8 Avatar asked Jan 25 '13 12:01

equivalent8


1 Answers

private and public are Ruby's access modifiers:

class User
  private
  def some_private_method
  end

  public
  def some_public_method
  end
end

While they may seem like keywords, they are actually method calls. It's not really a good idea to overwrite them.

like image 199
delwyn Avatar answered Sep 22 '22 12:09

delwyn