ruby - Stub pass parameter to scope -
i have scope in model looks like:
scope :public, -> { another_scope.where(v_id: 1) }
when stub model in tests:
model.stub(:test).and_return(test)
it passes value scope receive
wrong number of arguments (1 0)
how can avoid this? when change to:
scope :public, ->(arg) { another_scope.where(v_id: 1) }
it works fine, arg never used
it works fine when don't use lambda ex:
scope :public, another_scope.where(v_id: 1)
use proc instead of lambda.
scope :public, proc{ another_scope.where( v_id: 1 ) }
lambdas "strict" kind of proc require right amount of arguments.
alternatively, here's little hack if want keep 'stabby lambda' syntax (though it's not readable, , looks oddly disturbing me, miniature eye of sauron):
scope :public, ->(*){ another_scope.where( v_id: 1 ) }
the splat functions same way when use in method signatures def foo( *args ); end
, except args don't captured in variable.
Comments
Post a Comment