我正在嘗試為我的模型中的范圍撰寫測驗。
it "returns user that are manager" do
user = FactoryBot.create(:user, manager: true)
expect(User.is_manager(true)).to include(user)
end
it "returns user that are not manager" do
user = FactoryBot.create(:user, manager: false)
expect(User.is_manager(false)).to include(user)
end
這真的很簡單,但我有接近 20 個這樣的方法
我想做的是更接近于此的事情
describe 'scopes' do
[
{name: :is_manager, column: :manager},
{name: :is_foo, column: :foo},
{name: :can_baz, column: :baz}
].each do |scope|
it "returns user that are #{scope[:column]}" do
user = FactoryBot.create(:user, scope[:column] true) # this line is given me a prolem
expect(User::Permission.send(scope[:name](true)).to include(user)
end
end
end
uj5u.com熱心網友回復:
在你的情況下,我可以這樣做
describe 'user scopes' do
{
:is_manager => [[true], {manager: true}],
:is_foo => [[], {foo: true}],
:not_foo => [[], {foo: false}],
:by_name => [["%name%"], {name: "a name"}]
}.each do |scope, (scope_args, model_args)|
it "#{scope} should returns appropriate users" do
user = FactoryBot.create(:user, **model_args)
expect(User::Permission.send(scope, *scope_args)).to include(user)
end
end
end
或者你可以改變
{
[:is_manager, [true]] => {manager: true}
}.each do |(scope, scope_args), model_args|
# ...
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/314949.html
