我有一個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/[email protected]/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).>>.
同樣,正如我所說的,一切正常,當你實際運行的應用程序,它只是讓測試工作(雖然也許應用程序是唯一WO誰知道呢?)。
簽署你怎麼了檢查用戶是否登錄?你爲什麼發送':agency_id'作爲會話變量? – Bohdan
我想我不檢查,因爲他們應該已經在每次測試之前通過設置登錄,並且它可以在其他測試中工作,並且我有一個測試,只有當我在它之前執行'signout @ user'時纔會通過測試。至於':agency_id',用戶可能有多個代理機構,但他們應該只與一個機構進行交互,所以它似乎是一種傳遞信息的好方法。仍然在學習rails,所以這可能不是最好的方法。 –
我們可以看到sign_in方法的定義嗎? – Jakobinsky