1

テストがランダムに成功して失敗するという非常に奇妙な問題があります。つまり、合格することもあれば、失敗することもあります。これは、paperclip-matchers ( https://github.com/thoughtbot/paperclip ) と shoulda-matchers ( https://github.com/thoughtbot/shoulda-matchers ) の問題です。

次のように記述されたモデルがあるとします。

class Import < ActiveRecord::Base
  has_attached_file :document
  validates_attachment :document, presence: true,
                       content_type: { content_type: ['application/vnd.ms-excel', 'application/csv', 'text/csv'] }
end

そして、次のように書かれたテスト:

require 'spec_helper'   

describe Import do
  it { should have_attached_file(:document) }
  it { should validate_attachment_presence(:document) }
  it { should validate_attachment_content_type(:document).
    allowing('application/vnd.ms-excel', 'application/csv', 'text/csv') }
end

私が得るエラーは次のとおりです。

Failures:   

  1) Import should require presence of attachment document
     Failure/Error: it { should validate_attachment_presence(:document) }
     NoMethodError:
       undefined method `gsub' for nil:NilClass
     # ./spec/models/import_spec.rb:14:in `block (2 levels) in <top (required)>'

私はこれについてグーグルで調べてきましたが、なぜ壊れているのかわかりません。spec_helper.rb のドキュメントに従って適切に必要な paperclip-matchers を使用しているだけです。

誰かがこの問題に遭遇しましたか?

ありがとうございました!

編集:

今回は別のモデルで発生したことを除いて、最終的にバックトレースを取得しました。これがバックトレースです。paperclip マッチャーが最新バージョンの rspec と互換性がないことが問題のようです。以前にこの問題に遭遇したことがあり、この特定の問題に対して提案できる回避策があるでしょうか?

   1) Element should require presence of attachment attachment
      Failure/Error: it { should validate_attachment_presence :attachment }
      NoMethodError:
        undefined method `gsub' for nil:NilClass
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/io_adapters/abstract_adapter.rb:23:in `original_filename='
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/io_adapters/stringio_adapter.rb:13:in `cache_current_values'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/io_adapters/stringio_adapter.rb:5:in `initialize'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/io_adapters/registry.rb:29:in `new'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/io_adapters/registry.rb:29:in `for'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/attachment.rb:96:in `assign'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/matchers/validate_attachment_presence_matcher.rb:48:in `no_error_when_valid?'
      # /usr/local/lib/ruby/gems/1.9.1/gems/paperclip-4.2.0/lib/paperclip/matchers/validate_attachment_presence_matcher.rb:22:in `matches?'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-3.0.2/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/memoized_helpers.rb:81:in `should'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `map'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block in run_specs'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/reporter.rb:54:in `report'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:108:in `run_specs'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:86:in `run'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in `run'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:in `invoke'
      # /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-3.0.2/exe/rspec:4:in `<top (required)>'
      # /usr/local/bin/rspec:23:in `load'
      # /usr/local/bin/rspec:23:in `<main>'
4

2 に答える 2

0

ここで答えを見つけました

ただし、インスタンス メソッドを参照する path 属性にシンボルを渡すことができると述べている、paperclip gems の readme に反映されているとは思わない小さなメモがありました。コードをラムダからパブリック プライベート メソッドに移動し、パス属性に渡される値としてそのメソッド名を使用するのは魅力的でした。

:path => :path_to_file


def path_to_file
  return "/system/#{sub_domain}/:class/:id/:basename.:extension"
end

private
  def sub_domain
    ...code to get to the sub_domain of the school
  end
于 2014-07-13T05:14:00.663 に答える