Ruby on rails 3 可以在应用程序中工作,但不能在测试中工作
我的ability.rb文件中有以下定义:Ruby on rails 3 可以在应用程序中工作,但不能在测试中工作,ruby-on-rails-3,factory-bot,cancan,Ruby On Rails 3,Factory Bot,Cancan,我的ability.rb文件中有以下定义: can :index, Call, :country_id => user.countries 我试图只索引国家id在当前用户的国家/地区数组中的呼叫 当我尝试它时,它似乎起了作用:它只显示当前用户所属国家/地区标记的电话 然而,我有一个似乎无法通过的测试: before :each do # Users @admin = FactoryGirl.create(:user, admin: true) end
can :index, Call, :country_id => user.countries
我试图只索引国家id在当前用户的国家/地区数组中的呼叫
当我尝试它时,它似乎起了作用:它只显示当前用户所属国家/地区标记的电话
然而,我有一个似乎无法通过的测试:
before :each do
# Users
@admin = FactoryGirl.create(:user, admin: true)
end
it "should show the call index page with just calls that have the same country tags as them" do
anotheruser1 = FactoryGirl.create(:user)
anotheruserscall1 = FactoryGirl.create(:call, user_id: anotheruser1.id)
anotheruser2 = FactoryGirl.create(:user)
anotheruserscall2 = FactoryGirl.create(:call, user_id: anotheruser2.id)
@admin.countries << anotheruserscall1.country
ability = Ability.new(@admin)
ability.should be_able_to(:index, anotheruserscall1)
ability.should_not be_able_to(:index, anotheruserscall2)
end
before:每个人都做
#使用者
@admin=FactoryGirl.create(:user,admin:true)
结束
它“应该显示呼叫索引页面,其中只显示与它们具有相同国家/地区标记的呼叫”
anotheruser1=FactoryGirl.create(:user)
另一个UserScal1=FactoryGirl.create(:call,user\u id:antherUser1.id)
anotheruser2=FactoryGirl.create(:user)
AnotherUserScal2=FactoryGirl.create(:call,user\u id:anotheruser2.id)
@admin.countries好的,我通过将详细信息作为一个块传递进来解决了这个问题,如下所示:
can :index, Call, ["country_id IN (?)", user.countries] do |call|
user.countries.include?(call.country)
end
can :index, Call, ["country_id IN (?)", user.countries] do |call|
user.countries.include?(call.country)
end