验证失败:使用rspec上传徽标时徽标不能为空

时间:2018-11-13 16:19:51

标签: ruby rspec carrierwave factory-bot

我正在使用rspec和factoryBot为我的Rails应用程序创建自动化测试。就我而言,我正在创建一家拥有徽标的公司。在测试中创建公司不是问题,但是如果我尝试在测试文件之外修改公司,则会收到

的错误消息
ActiveRecord::RecordInvalid:
   Validation failed: Logo can't be blank

我尝试了其他上传方法,但没有运气。以下是公司的工厂以及我用来创建公司的过程。这不是我的实现中的确切代码,因为我无法共享它,但是我已尽可能地复制了它。

如果我手动测试代码,则可以正常工作。仅当使用rspec运行代码时,才会发生错误。

factories.rb

FactoryBot.define do
  factory :company do
    name { 'Name' }
    b_day { Date.today }
    age { 1 }
    logo { Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/support/logo.png')), 'image/png') }
  end
end

companies_rake_spec.rb

describe "Company b_day today" do
  it "Add 1 to company age" do
    company1 = create(:company)
    Rake::Task['companies:update_age'].invoke

    company1_refresh = Company.where(id: company1.id).first
    expect(company1_refresh.age).to eq(company1.age + 1)
  end
end

company.rake

namespace :companies do
  desc "TODO"
    task update_age: :environment do
       Company.find_each do | company |
          if (company.b_day.day == Date.today.day) && (company.b_day.month == Date.today.month)
            company.age += 1
            puts company.inspect
            company.save!
          end
       end
    end
  end
end

控制台

#<Company id: 34, name: "Name", b_day: 2018-11-13, age: 2, logo: "1cbeef36f13a8a88db0c37fa30da6ffd.png">

ActiveRecord::RecordInvalid:
   Validation failed: Logo can't be blank

stacktrace

Failures:

1) companies:update_age age + 1
 Failure/Error: company.save!

 ActiveRecord::RecordInvalid:
   Validation failed: Logo can't be blank
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:78:in `raise_validation_error'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:50:in `save!'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `block in save!'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:211:in `transaction'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `save!'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/suppressor.rb:45:in `save!'
 # ./lib/tasks/companies.rake:54:in `block (3 levels) in <top (required)>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:59:in `block (2 levels) in find_each'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:59:in `each'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:59:in `block in find_each'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:121:in `block in find_in_batches'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:214:in `block in in_batches'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:198:in `loop'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:198:in `in_batches'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:120:in `find_in_batches'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/relation/batches.rb:58:in `find_each'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/activerecord-5.0.7/lib/active_record/querying.rb:9:in `find_each'
 # ./lib/tasks/companies.rake:8:in `block (2 levels) in <top (required)>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
 # /Users/mac/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
 # ./spec/lib/tasks/companies_rake_spec.rb:122:in `block (3 levels) in <top (required)>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:254:in `instance_exec'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:254:in `block in run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:464:in `block in run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:604:in `block in run_around_example_hooks_for'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:342:in `call'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-rails-3.8.0/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:447:in `instance_exec'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:373:in `execute_with'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:342:in `call'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/hooks.rb:464:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example.rb:251:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:629:in `block in run_examples'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:625:in `map'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:625:in `run_examples'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:591:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:592:in `block in run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:592:in `map'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/example_group.rb:592:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:116:in `block (3 levels) in run_specs'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:116:in `map'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:116:in `block (2 levels) in run_specs'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/configuration.rb:1989:in `with_suite_hooks'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:111:in `block in run_specs'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/reporter.rb:74:in `report'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:110:in `run_specs'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:87:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:71:in `run'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/lib/rspec/core/runner.rb:45:in `invoke'
 # /Users/mac/.rvm/gems/ruby-2.5.1/gems/rspec-core-3.8.0/exe/rspec:4:in `<top (required)>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/bin/rspec:23:in `load'
 # /Users/mac/.rvm/gems/ruby-2.5.1/bin/rspec:23:in `<main>'
 # /Users/mac/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `eval'
 # /Users/mac/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `<main>'

1 个答案:

答案 0 :(得分:0)

How to properly do model testing with ActiveStorage in rails?

使用此处的信息并适应您的具体情况,请尝试以下工厂:

FactoryBot.define do factory :company do name { 'Name' } b_day { Date.today } age { 1 } after(:build) do |company| company.logo.attach(io: File.open(Rails.root.join('spec', 'support', 'logo.jpeg')), filename: 'logo.jpeg', content_type: 'image/jpeg') end end end

编辑:

您可能还需要将其添加到config / environments / test.rb文件中:

config.active_storage.service = :test