0

Railscasts 認証を自分のアプリに最初から実装しようとしていますが、カスタム マッチャーとテストで単純な問題が発生しています。引数の数が間違っているというエラーが表示されますが、その理由がわかりません。

すべてのロジックを保持する別の Permission モデルを作成しました。このモデルには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
4

1 に答える 1

1

allowhttps://github.com/rspec/rspec-mocks/blob/fd78578d0d65a7917701c4410a0eb9089ee6636f/lib/rspec/mocks/syntax.rbで定義されている RSpec メソッドで、1 つのパラメーターを取ります。そのため、このエラーが発生しています。

これを確認するには、次の例を実行します。

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/ruby-2.0.0-p247@botmetrics/gems/rspec-mocks-2.14.3/lib/rspec/mocks/syntax.rb 117

別のマッチャー名を使用する場合は、問題ないと思います。

于 2013-10-25T16:59:13.380 に答える