carrierwave be_identical_to helper在rspec中不起作用

时间:2013-03-21 16:26:01

标签: ruby ruby-on-rails-3 rspec carrierwave rspec-rails

我在使用rspec测试时遇到一些问题我正试图在Carrierwave上传中运行。基本上,我正在尝试测试处理以确保图像是否上传和处理。我创建了一个后处理的示例文件,该文件应与上传后处理的测试文件相同。但是,我收到以下警告:


ImageUploader the greyscale version should remove color from the image and make it greyscale
 Failure/Error: @uploader.should be_identical_to(@pregreyed_image)
 TypeError:
   can't convert ImageUploader into String
 # ./spec/uploaders/image_uploader_spec.rb:24:in `block (3 levels) in <top (required)>'

这是我的测试文件:

image_uploader_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'
require 'carrierwave/test/matchers'

describe ImageUploader do
  include CarrierWave::Test::Matchers
  include ActionDispatch::TestProcess

  before do
    ImageUploader.enable_processing = true
    @uploader_attr = fixture_file_upload('/test_images/testimage.jpg',    'image/jpeg')
    @uploader = ImageUploader.new(@uploader_attr)
    @uploader.store!
    @pregreyed_image =             fixture_file_upload('/test_images/testimage_GREY.jpg', 'image/jpeg')
  end

  after do
    @uploader.remove!
    ImageUploader.enable_processing = false
  end

  context 'the greyscale version' do
    it "should remove color from the image and make it greyscale" do

      @uploader.should be_identical_to(@pregreyed_image)
    end
  end
  end

image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  storage :file


  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


  # Process files as they are uploaded:
   process :convert_to_grayscale

  def convert_to_grayscale
    manipulate! do |img|
      img.colorspace = Magick::GRAYColorspace
      img.quantize(256, Magick::GRAYColorspace)
      img = yield(img) if block_given?
      img
    end
  end

1 个答案:

答案 0 :(得分:0)

封面be_identical_to下面的两个参数使用FileUtils.identical?。所以你的期望:

@uploader.should be_identical_to(@pregreyed_image)

实际上是在呼叫:

FileUtils.identcal?(@uploader, @pregreyed_image)

因为在我的测试环境中我使用的是文件存储系统,所以我通过传递#current_path而非上传者本身来解决这个问题:

@uploader.current_path.should be_identical_to(@pregreyed_image)

我实际上最终需要直接比较上传器并在我的上传器上实现==

class MyUploader < CarrierWave::Uploader::Base
  ...

  def ==(other)
    return true if !present? && !other.present?
    FileUtils.identical?(current_path, other.current_path)
  end
end