加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RSpec讨厌Rails范围,命名为:public,当存根对

发布时间:2020-12-17 03:15:41 所属栏目:百科 来源:网络整理
导读:当我命名我的rails模型范围时,它只是我还是全局RSpec行为:public,从此模型初始化对象,以及存根此对象Rspec失败 class DocumentName ActiveRecord::Base scope :public,lambda{where( public: true) } #line 3end 没什么特别的,Rails应用程序可行 DocumentNa
当我命名我的rails模型范围时,它只是我还是全局RSpec行为:public,从此模型初始化对象,以及存根此对象Rspec失败

class DocumentName < ActiveRecord::Base

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

没什么特别的,Rails应用程序可行

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

但是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)>'

……最有趣的是,在这种情况下,我没有对该范围做任何事情.

但是,如果我将此范围命名为:public例如:are_public:

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

……一切都通过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)

解决方法

私有和公共是Ruby的访问修饰符:

class User
  private
  def some_private_method
  end

  public
  def some_public_method
  end
end

虽然它们看起来像关键字,但它们实际上是方法调用.覆盖它们并不是一个好主意.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读