Ruby 存根传递参数到作用域

Ruby 存根传递参数到作用域,ruby,ruby-on-rails-3.2,rspec2,Ruby,Ruby On Rails 3.2,Rspec2,我的模型中有如下范围: scope :public, -> { another_scope.where(v_id: 1) } 当我在测试中存根此模型时: model.stub(:test).and_return(test) 它将一个值传递到此范围,以便我接收 wrong number of arguments (1 for 0) 我怎样才能避免这种情况? 当我将其更改为: scope :public, ->(arg) { another_scope.where(v_id: 1)

我的模型中有如下范围:

scope :public, -> { another_scope.where(v_id: 1) }
当我在测试中存根此模型时:

model.stub(:test).and_return(test)
它将一个值传递到此范围,以便我接收

wrong number of arguments (1 for 0)
我怎样才能避免这种情况? 当我将其更改为:

scope :public, ->(arg) { another_scope.where(v_id: 1) }
它工作正常,但从未使用arg

当我不使用lambda ex时,它也可以正常工作:

scope :public, another_scope.where(v_id: 1)
使用a而不是lambda。

scope :public, proc{ another_scope.where( v_id: 1 ) }
lambda是一种“严格”的过程,需要适当数量的参数

或者,如果你想保留“Thorky lambda”语法,这里有一个小技巧(尽管它不是那么易读,而且看起来奇怪地让我不安,就像索伦的一只微型眼睛):

splat的工作方式与在方法签名中使用它时完全相同,如
def foo(*args);结束
,除非参数没有被捕获到变量中

scope :public, ->(*){ another_scope.where( v_id: 1 ) }