2013-10-25 55 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