简体   繁体   English

当存根对象时,RSpec讨厌名为:public的Rails范围

[英]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 当我将我的rails模型范围命名为:public ,是我还是它的全局行为?

class DocumentName < ActiveRecord::Base

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

nothing special, Rails application works 没什么特别的,Rails应用程序有效

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

however RSpec fails 但是RSpec失败

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 eg: :are_public : 但是,如果我将此范围命名为:public以外的其他名称,例如:are_public

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

...everything pass O_O ...一切顺利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)

private and public are Ruby's access modifiers: privatepublic是Ruby的访问修饰符:

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. 覆盖它们并不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM