簡體   English   中英

回形針應當匹配器錯誤(nil:NilClass的未定義方法“ gsub”)

[英]Paperclip Shoulda Matcher error (undefined method `gsub' for nil:NilClass)

我有一個非常奇怪的問題,即測試通過並隨機失敗。 意思是有時會通過,有時會失敗。 這是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中的文檔我已經正確地要求了

有人遇到過這個問題嗎?

謝謝!

編輯:

終於得到了回溯,除了這次是在另一個模型上發生的。 這是回溯。 回形針匹配器與最新版本的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>'

我在這里找到答案

但是,有一點需要注意的是,我不認為回形針gem自述文件中會回顯這一點,它指出可以將符號傳遞給引用實例方法的path屬性。 將我的代碼從lambda移到公共私有方法,並使用該方法名稱作為傳遞給path屬性的值,就像一個魅力。

: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

我實際上通過使用回形針的master分支解決了該問題。 有點奇怪,因為版本號似乎相同。 所以基本上,在Gemfile中:

gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM