1

我有一个非常奇怪的问题,即测试随机通过和失败。意思是,有时它会通过,有时它会失败。这是回形针匹配器 ( https://github.com/thoughtbot/paperclip ) 与 shoulda-matchers ( https://github.com/thoughtbot/should-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>'
4

2 回答 2

0

我在这里找到了答案

但是,有一点我不相信在回形针宝石自述文件中得到回应,它指出可以将符号传递给引用实例方法的路径属性。将我的代码从 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
于 2014-07-13T05:14:00.663 回答
0

我实际上通过使用回形针的主分支解决了这个问题。这有点奇怪,因为版本号似乎相同。所以基本上,在 Gemfile 中:

gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"
于 2014-07-14T13:22:10.037 回答