2016-08-03 57 views
2

我將Rails 3.2中的應用程序轉換爲使用cancan寶石的4.2。谷歌搜索和檢查他們的github說,它只是代替gem cancangem cancancan而不會改變任何東西。這似乎並不奏效。我的測試失敗:正在搜索可以在洛後手動達到Capybara :: Webkit :: InvalidResponseError:Javascript未能執行

Failures: 

    1) Redactable fields Manual redaction Redacting selected text in body 
    Failure/Error: 
      page.execute_script <<-EOS 
      document.getElementById('notice_#{@name}').focus(); 
      document.getElementById('notice_#{@name}').select(); 
      EOS 

    Capybara::Webkit::InvalidResponseError: 
     Javascript failed to execute 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:305:in `check' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:211:in `command' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:232:in `execute_script' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:80:in `execute_script' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/session.rb:524:in `execute_script' 
    # ./spec/support/page_objects/redactable_field_on_page.rb:15:in `select' 
    # ./spec/support/page_objects/redactable_field_on_page.rb:22:in `select_and_redact' 
    # ./spec/integration/redaction_spec.rb:37:in `block (4 levels) in <top (required)>' 

    2) Redactable fields Manual redaction Restoring body from original 
    Failure/Error: page.find("#notice_#{@name}").click 

    Capybara::ElementNotFound: 
     Unable to find css "#notice_body" 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find' 
    # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>' 
    # ./spec/support/page_objects/redactable_field_on_page.rb:9:in `unredact' 
    # ./spec/integration/redaction_spec.rb:46:in `block (4 levels) in <top (required)>' 

元素這意味着既不cancancan也不cancan成功應用於:admin許可,這是一個在測試中創建我的用戶。我用You are not authorized to access this page.,我得到「您無權訪問此頁面。」

的規範本身看起來是這樣的:

require 'rails_helper' 
require 'support/notice_actions' 

     #lots of irrelevant code 

     private 

     def visit_redact_notice 
     user = create(:user, :admin) 
     visit '/users/sign_in' 
     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_on "Log in" 

     notice = create(:dmca, :redactable) 

     visit "/admin/notice/#{notice.id}/redact_notice" 

     notice 
     end 
    end 
    end 
end 

visit_redact_notice方法在上面的規格是這裏的一切正在發生。我需要幫助 :)。這是我的回購協議,如果它可以幫助:https://github.com/siaw23/lumendatabase

ability.rb

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    return unless user 

    if user.has_role?(Role.submitter) 
     can :submit, Notice 
    end 

    if user.has_role?(Role.redactor) 
     grant_admin_access 
     grant_redact 
    end 

    if user.has_role?(Role.publisher) 
     grant_admin_access 
     grant_redact 

     can :publish, Notice 
    end 

    if user.has_role?(Role.admin) 
     grant_admin_access 
     grant_redact 

     can :edit, :all 
     cannot :edit, [User, Role] 

     can :publish, Notice 
     can :rescind, Notice 

     can :pdf_requests, :all 
    end 

    if user.has_role?(Role.super_admin) 
     can :manage, :all 
    end 

    if user.has_role?(Role.researcher) 
     can :read, Notice 
    end 
    end 

    def grant_admin_access 
    can :read, :all 
    can :access, :rails_admin 
    can :dashboard 
    can :search, Entity 
    can :access, :original_files 
    end 

    def grant_redact 
    can :edit, Notice 
    can :redact_notice, Notice 
    can :redact_queue, Notice 
    end 
end 
+2

capybara-webkit 1.0.0是2.5歲 - 我會建議更新它。 –

+0

我更新了'capybara-webki'到最新版本。看起來像是別的東西。 – Emanuel

+0

如果你不確定發生了什麼事(如果你是驅動程序支持的話),通常會有助於拋出一個'page.save_screenshot('whatever.png')' –

回答

1

你可能想看看一對夫婦的問題...

  1. 有設計的新版本指出,允許通過功能/集成測試進行驗證。我總是花幾個小時調試實際上逐步登錄功能工作。使用Devise 4.2,現在支持logging in via integration tests。您必須對Devise的RSpec配置進行一些更改,如上面關於sign_in方法工作的文檔所述。
  2. 如果此時以管理員身份登錄仍不起作用,我會檢查您的RSpec配置是否已將use_transactional_fixtures設置爲true。如果是這樣,我強烈建議您讓database_cleaner寶石管理您的測試數據庫,並將use_transactional_fixtures設置爲false

其中一個或兩個應該讓你繞過你的認證問題。我的猜想是它最有可能是交易裝置的問題,因爲Rails可能會在您完全驗證之前清理一個事務(因此我懷疑上面的#1會失效)。