全部。 我很难用默认范围为我的模型编写测试。我有一个具有属性 active_status 的用户模型,可以由管理员更改。我给了用户默认范围 active_status true。 default_scope(:active_status => true ) 在 Admin 页面中,我使用了 unscoped 以便 Admin 可以看到所有用户。 这个测试怎么写? 谢谢
最佳答案
你可以这样做:
context "scopes" do
describe "default_scope" do
before do
create(:user, active_status: true)
create(:user, active_status: false)
}
context "with default scope" do
subject { User.all }
it { should have(1).user }
end
context "without default scope" do
subject { User.unscoped.all }
it { should have(2).users }
end
end
end
注意:我正在使用 RSpec和 FactoryGirl .
注意 2:您可以将此视为反模式,因为 default_scope 是 protected 方法。但这正是您所要求的。
关于ruby-on-rails-3 - 在 Rails 中测试默认范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915483/