我有一个正在开发的 Ruby on Rails 应用程序,但我的功能测试遇到了一些问题。特别是,当我通过具有类似凭据(相同角色等)的用户登录时,我在测试期间不断被拒绝访问可以在浏览器中访问的页面。例如,下面是控制器测试中的代码:
include Devise::TestHelpers
include Authorization::TestHelper
...
setup do
@user = Factory(:user)
@user.roles << Factory(:refinery_role)
@user.roles << Factory(:agency_role)
@user.save
sign_in @user
@agency = AgencyOrganization.create :name => "Test Agency"
@adv1 = AdvertiserOrganization.create :name => "Test Advertiser", :parent => @agency
UserOrganization.create :user_id => @user.id, :organization_id => @agency.id
end
test "agency user can edit advertiser" do
assert @user.has_role? :agency #passes
should_be_allowed_to :update, :advertiser_organizations #passes
get :edit, {:id => @adv1.id}, {:agency_id => @agency.id}
assert_equal "/unauthorized", request.env['PATH_INFO'] #passes :'(
assert_template :edit #fails
# and more tests we never get to
end
(显然,这些并不是我真正想要检查的所有断言,但它们证明了正在发生的事情。)
对于它的价值,上述测试失败并引发以下异常:
4) Failure:
test_agency_user_can_edit_advertiser(AdvertiserOrganizationsControllerTest [/Users/gworley/.rvm/gems/ruby-1.9.2-p180@portal/gems/declarative_authorization-0.5.1/lib/declarative_authorization/maintenance.rb:170]:
Exception raised:
<#<Authorization::NotAuthorized: No matching rules found for update for #<Authorization::GuestUser:0x00000101cda2b0 @role_symbols=[:guest]> (roles [:guest], privileges [:update, :manage], context :advertiser_organizations).>>.
同样,正如我所说,当您实际运行应用程序时,一切正常,它只是让测试工作(尽管也许应用程序只是偶然工作,谁知道呢?)。