2013-05-14 62 views
0

我在我的模型範圍,它看起來像:存根通參數範圍

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) 

回答

1

使用Proc而不是lambda。

scope :public, proc{ another_scope.where(v_id: 1) } 

lambda表達式是一個「嚴」之類的進程內需要的參數各適量。

或者,這裏有一個小黑客,如果你想保持「stabby拉姆達」的語法(儘管它不是那麼可讀,看起來奇怪令人不安的我,就像索倫的微型眼):

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

splat的功能與在方法簽名中使用它的方式完全相同,如def foo(*args); end,但args不會被變量捕獲。