2013-10-25 58 views
0

我試圖在自己的應用程序中從頭開始實施Railscasts授權,但是我在自定義匹配器和測試中遇到了一個簡單問題。我得到錯誤的參數錯誤數目,我似乎無法弄清楚爲什麼。Rails中的測試授權問題

我創建了一個單獨的權限模型來保存所有的邏輯,在此模型中有一個allow?方法。這就是所有的邏輯所在。我手動測試它通過瀏覽器和它的作品不過我的單元測試失敗是由於錯誤的參數數目

wrong number of arguments (2 for 1) 

這是權限模型

class Permission < Struct.new(:user) 
    def allow?(controller, action) 
    return true if controller == 'sessions' 
    return true if controller == 'users' && action.in?(%w[new create]) 
    if user && user.teams.nil? 
     return true if controller == 'users' && action.in?(%w[new index show edit update]) 
     return true if controller == 'teams' && action.in?(%w[new create index]) 
     return true if controller == 'user_teams' && action.in?(%w[index]) 
    elsif user && !user.teams.nil? 
     return true if controller == 'users' && action.in?(%w[index show edit update]) 
     return true if controller == 'teams' && action.in?(%w[new create index show]) 
     return true if controller == 'user_teams' && action.in?(%w[index]) 
     return true if controller == 'texts' && action.in?(%w[new create index show]) 
     return true if controller == 'translations' && action.in?(%w[show]) 

    end 
    end 
end 

這裏是位於自定義匹配的代碼spec/support/matchers/allow.rb

RSpec::Matchers.define :allow do |*args| 
    match do |permission| 
    permission.allow?(*args).should be_true 
    end 

    failure_message_for_should do |permission| 
    "expected to have permission for these actions" 
    end 

    failure_message_for_should_not do |permission| 
    "expected to not have permission for these actions" 
    end 

    description do 
    "allow access to the %{*args} actions" 
    end 
end 

這裏是我的應用程序控制器

class ApplicationController < ActionController::Base 
    before_filter :authorize 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    include SessionsHelper 
    include UserTeamsHelper 

    add_flash_types :success, :danger 

    private 

    def current_permission 
    @current_permission ||= Permission.new(current_user) 
    end 

    def authorize 
    if !current_permission.allow?(params[:controller], params[:action]) 
     redirect_to root_url, danger: 'Önce Giriş Yapmalısınız' 
    end 
    end 

end 

這裏是權限模型

require "spec_helper" 

describe Permission do 
    describe 'as a non user' do 
    subject { Permission.new(nil) } 
    it { should allow("sessions", "new") } 
    end 
end 

測試但它不工作,因爲我得到一個錯誤的號碼參數錯誤。有人能指出我測試這些的正確途徑嗎?

下面是測試失敗我正在

Failures: 

    1) Permission as a non user 
    Failure/Error: it { should allow("sessions", "new") } 
    ArgumentError: 
     wrong number of arguments (2 for 1) 
    # ./spec/models/permission_spec.rb:6:in `block (3 levels) in <top (required)>' 

Finished in 0.07471 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/models/permission_spec.rb:6 # Permission as a non user 
+0

難道你不能發佈堆棧跟蹤,所以我們可以確切知道哪一行拋出錯誤? – PinnyM

回答

1

allowhttps://github.com/rspec/rspec-mocks/blob/fd78578d0d65a7917701c4410a0eb9089ee6636f/lib/rspec/mocks/syntax.rb這需要一個參數定義一個RSpec方法。這就是你得到這個錯誤的原因。

describe "RSpec allow method" do 
    it "should exist" do 
    puts method(:allow) 
    puts method(:allow).source_location 
    end 
end 

應該產生類似:

可以通過執行下面的示例驗證這一點

#<Method: RSpec::Core::ExampleGroup::Nested_1(RSpec::Mocks::ExampleMethods)#allow> 
/Users/palfvin/.rvm/gems/[email protected]/gems/rspec-mocks-2.14.3/lib/rspec/mocks/syntax.rb 117 

如果使用不同的匹配名字,我想你會沒事的。

+0

謝謝你解決了! – Brock90