2015-05-08 11 views
0

我有責任確保用戶有權查看的頁面的幫手存根current_or_guest_user,並重定向如果用戶沒有權限:無法在我的幫手規格

module PermissionsHelper 
    def require_permission(permission_attribute_name) 
    return if current_or_guest_user.role.send(permission_attribute_name) 
    redirect_to current_or_guest_user.role.landing_page, 
       notice: 'You do not have sufficient permissions' 
    end 
end 

方法current_or_guest_user是方法我在另一個返回當前用戶的幫助器中使用,或者在沒有當前用戶的情況下創建並返回一個訪客。

我的規格如下所示:

require 'rails_helper' 

RSpec.describe PermissionsHelper, type: :helper do 
    describe 'requiring permissions' do 
    let(:test_user) { create :customer } 

    it "redirects the user to the user's landing page if the user doesn't have permission" do 
     allow(helper).to receive(:current_or_guest_user) { test_user } 
     require_permission(:view_admins) 
     expect(response).to redirect_to test_user.landing_page 
    end 
    end 
end 

而且我收到此錯誤:

PermissionsHelper 
    requiring permissions 
    redirects the user to the user's langing page if the user doesn't have permission (FAILED - 1) 

Failures: 

    1) PermissionsHelper requiring permissions redirects the user to the user's langing page if the user doesn't have permission 
    Failure/Error: require_permission(:view_admins) 
    NameError: 
     undefined local variable or method `current_or_guest_user' for #<RSpec::ExampleGroups::PermissionsHelper::RequiringPermissions:0x007fb7b430afe8> 
    # /Users/user/.rvm/gems/[email protected]/gems/actionpack-4.2.1/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing' 
    # /Users/user/.rvm/gems/[email protected]/gems/actionview-4.2.1/lib/action_view/test_case.rb:271:in `method_missing' 
    # ./app/helpers/permissions_helper.rb:3:in `require_permission' 
    # ./spec/helpers/permissions_helper_spec.rb:11:in `block (3 levels) in <top (required)>' 
    # /Users/user/.rvm/gems/[email protected]/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:43:in `block (3 levels) in apply' 
    # /Users/user/.rvm/gems/[email protected]/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:34:in `times' 
    # /Users/user/.rvm/gems/[email protected]/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:34:in `block (2 levels) in apply' 

Finished in 0.01188 seconds (files took 1.88 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/helpers/permissions_helper_spec.rb:9 # PermissionsHelper requiring permissions redirects the user to the user's langing page if the user doesn't have permission 

編輯:我試圖改變allowallow_any_instance_of,現在我得到這個錯誤:

有了這個規格:

it "redirects the user to the user's langing page if the user doesn't have permission" do 
    allow_any_instance_of(helper).to receive(:current_or_guest_user) { test_user } 
    require_permission(:view_admins) 
    expect(response).to redirect_to test_user.landing_page 
end 

我得到:

Failure/Error: allow_any_instance_of(helper).to receive(:current_or_guest_user) { test_user } 
    NoMethodError: 
     undefined method `ancestors' for #<#<Class:0x007fe5d19a9a98>:0x007fe5d19a1668> 

回答

0
allow 

將只使用靜態方法的工作。由於在助手的方法是一個實例方法,你反而會希望使用

allow_any_instance_of 

https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations/allow-a-message-on-any-instance-of-a-class

編輯:

以下代碼段在我的項目之一(使用「助手」失敗變量)

allow_any_instance_of(helper).to receive(:paginate).and_return('asdf') 

然而,使用完整的類名正常工作(用於測試方法的目的嘲笑至少)

allow_any_instance_of(PaginationHelper).to receive(:paginate).and_return('asdf) 
+0

我不確定這是否正確,因爲我在過去使用過實例方法並且沒有問題。無論哪種方式,我遇到了allow_any_instance_of錯誤,並將其發佈到原始文章中。 – ThomYorkkke

+0

我不確定在這個rspec上下文中'helper'的用途是什麼,但是當使用正確的類名時,它似乎表現得像預期的那樣。我更新了我的帖子以反映我的意思。 –

+0

它仍然無法正常工作。我正在測試一個幫手,所以我不能說允許任何實例。我曾嘗試創建一個匿名類,並使其包含PermissionsHelper,但那也不起作用。 – ThomYorkkke