0

我的能力.rb 文件中有以下定义:

    can :index, Call, :country_id => user.countries

我试图仅索引其 country_id 在当前用户的国家/地区数组中的呼叫。

当我尝试时,这似乎有效:它只向我显示由 current_user 所属的国家/地区标记的呼叫。

但是,我有以下似乎无法通过的测试:

  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

但我失败了:

Failures:

  1) Admin ability should show the call index page with just calls that have the same country tags as them
     Failure/Error: ability.should be_able_to(:index, anotheruserscall1)
       expected to be able to :index #<Call id: 1, subject: "Natus qui fuga ut.", description: "Consequatur aut veritatis earum dolor. Reprehenderi...", from: "2012-10-25 18:43:23", to: "2012-10-25 19:43:23", user_id: 7, status: "Tentative", created_at: "2012-10-29 13:41:07", updated_at: "2012-10-29 13:41:07", expert_id: nil, country_id: 1>
     # ./spec/models/ability_spec.rb:291:in `block (2 levels) in <top (required)>'

非常感谢任何帮助。

4

1 回答 1

0

好的,我通过将详细信息作为块传递来解决此问题,如下所示:

    can :index, Call, ["country_id IN (?)", user.countries] do |call|
      user.countries.include?(call.country)
    end
于 2012-10-29T15:55:37.387 回答