我正在尝试为我的模型中的作用域编写一个测试。
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发布于 2021-10-09 07:59:43
对你来说,我可以这么做
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|
# ...
endhttps://stackoverflow.com/questions/69501648
复制相似问题