【问题标题】:Rails RSpec unit test fails on user attribute, image file typeRails RSpec 单元测试在用户属性、图像文件类型上失败
【发布时间】:2016-11-01 17:16:54
【问题描述】:

我已经尝试了几种不同的方法,但不清楚为什么我编写的用户单元测试的各种排列在测试用户对象的图像属性的文件类型时一直失败。

这是我目前拥有的...

models/user.rb

class User < ApplicationRecord
  mount_uploader :image, ImageUploader
  validate :image_size_validation
  validates_format_of :image, with: %r{\.(png|jpg|jpeg)\Z/}i, message: "image must be png, jpg, or jpeg", allow_nil: true

  def image_size_validation
    errors[:image] << "should be less than 5MB" if image.size > 5.megabytes
  end
end

上传者/image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

spec/models/user_spec.rb

  let(:user) { FactoryGirl.create(:user, username: "zoidberg", image: "image.png") }

  it "has a valid image file" do
    expect(user).to be_valid
    expect(user.image).to be_valid
    expect(FactoryGirl.build(:user, username: "Farnsworth", image: "image.jpg")).to be_valid
  end

令人费解的是,如果我不指定 username: &lt;something&gt;,则测试将失败并显示 Username is invalid,尽管它在所有其他测试中都有效并且已内置到工厂中。

spec/factories/user.rb

FactoryGirl.define do
  factory :user do
    first_name  Faker::Name.first_name
    last_name  Faker::Name.last_name
    sequence(:username) { |n| "#{n}#{Faker::Internet.user_name}#{Faker::Number.number(5)}#{Faker::Hipster.word}" }
    sequence(:email) { |n| "#{n}#{Faker::Hipster.word}#{Faker::Number.number(5)}#{Faker::Internet.email}" }
    phone  Faker::Base.numerify('##########')
    password  Faker::Internet.password(6, 20)
    state  Faker::Address.state_abbr
    city  Faker::Address.city
    image Faker::Avatar.image
    sequence(:zip) { |n| "#{n}#{n}#{n}#{n}#{n}" }
    seeking_coach true
    accept_email true
    accept_phone true
  end
end

我也试过这个it { should have_valid(:image).when('image.jpg', 'image.png', 'image.jpeg')},我认为它可以工作,但显然不行。

【问题讨论】:

  • be_valid 用于对象,而不是对象的属性。我想您的格式验证是因为在对象上调用图像将返回一个上传者,而不是字符串
  • 那么对象属性的适当语法是什么?
  • Rails 返回ActiveRecord::RecordInvalid: Validation failed: Image image must be png, jpg, or jpeg

标签: ruby-on-rails unit-testing rspec carrierwave


【解决方案1】:

您无法通过简单地使用字符串来表示文件 - “image.png”来测试carrierwave。您实际上需要打开文件并上传它。

File.open(path_to_file) { |f| user.image.store!(f) }

其中 path_to_file 表示您正在测试的文件的完整路径,包括文件名。

这就是为什么 shoulda 匹配器也不起作用的原因:

it { should have_valid(:image).when('image.jpg', 'image.png', 'image.jpeg')} # won't work

您需要编写单独的测试来检查每种文件类型。

您还应该构建用户对象 - 不要创建它,因为在此阶段它没有有效的图像:

let(:user) { FactoryGirl.build(:user, username: "zoidberg") }

it 'with png file it should be valid' do
  path_to_png_file = Rails.root + "spec/files/image.png"
  File.open(path_to_png_file) { |f| user.image.store!(f) }
  expect(user).to be_valid
end

it 'with txt file it should be invalid' do
  path_to_txt_file = Rails.root + "spec/files/text.txt"
  File.open(path_to_txt_file) { |f| user.image.store!(f) }
  expect(user).to_not be_valid
end

当然,您需要根据您的测试环境设置路径。

您不需要测试实际图像本身 - 它实际上只是您对其进行验证的用户的一个属性。如果图像无效,则用户无效,但您可以测试验证错误以确保它们与图像相关。

【讨论】:

  • 我讨厌这么愚蠢,但我很难实现这一点。当我在 rails c 中查询self.image.path 时,我会得到类似"uploads/Dr_John_Zoidberg.png" 的信息。所以我尝试了File.open("uploads/#{user.image}") 和File.open("#{user.image.path}") 以及其他排列,但它们将返回NameError: undefined local variable or method uploads' 或Errno::ENOENT: No such file or directory @ rb_sysopen - uploads/。
  • 您需要使用 File.open 以及系统上实际文件的完整路径 - 正如我在示例中所展示的那样。不要使用 user.image ...它是您尝试设置的。
猜你喜欢
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多