Validation failed: Logo can't be blank when logo uploaded using rspec
I am creating automated tests for my rails app using rspec and factoryBot. In my case I am creating a company with a requirement of having a logo. Creating a company in the test is not a problem but if I try and modify the company outside of the test file I am getting an error message of
ActiveRecord::RecordInvalid:
Validation failed: Logo can't be blank
I have tried alternative upload methods but with no luck. Below is the factory for the company and the process I am using to create a company. This is not the exact code in my implementation as I am unable to share that but I have replicated it as much as I could.
If I manually test the code it works fine. It is only when running the code with rspec the error occurs.
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
console
#<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>'
ruby rspec carrierwave factory-bot
add a comment |
I am creating automated tests for my rails app using rspec and factoryBot. In my case I am creating a company with a requirement of having a logo. Creating a company in the test is not a problem but if I try and modify the company outside of the test file I am getting an error message of
ActiveRecord::RecordInvalid:
Validation failed: Logo can't be blank
I have tried alternative upload methods but with no luck. Below is the factory for the company and the process I am using to create a company. This is not the exact code in my implementation as I am unable to share that but I have replicated it as much as I could.
If I manually test the code it works fine. It is only when running the code with rspec the error occurs.
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
console
#<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>'
ruby rspec carrierwave factory-bot
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
The command I ran wasrspec
. This called on a rake file which is included above. The point that error occurred wascompany.save!
. I have added the stacktrace above
– JackFurby
Nov 14 '18 at 9:33
add a comment |
I am creating automated tests for my rails app using rspec and factoryBot. In my case I am creating a company with a requirement of having a logo. Creating a company in the test is not a problem but if I try and modify the company outside of the test file I am getting an error message of
ActiveRecord::RecordInvalid:
Validation failed: Logo can't be blank
I have tried alternative upload methods but with no luck. Below is the factory for the company and the process I am using to create a company. This is not the exact code in my implementation as I am unable to share that but I have replicated it as much as I could.
If I manually test the code it works fine. It is only when running the code with rspec the error occurs.
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
console
#<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>'
ruby rspec carrierwave factory-bot
I am creating automated tests for my rails app using rspec and factoryBot. In my case I am creating a company with a requirement of having a logo. Creating a company in the test is not a problem but if I try and modify the company outside of the test file I am getting an error message of
ActiveRecord::RecordInvalid:
Validation failed: Logo can't be blank
I have tried alternative upload methods but with no luck. Below is the factory for the company and the process I am using to create a company. This is not the exact code in my implementation as I am unable to share that but I have replicated it as much as I could.
If I manually test the code it works fine. It is only when running the code with rspec the error occurs.
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
console
#<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>'
ruby rspec carrierwave factory-bot
ruby rspec carrierwave factory-bot
edited Nov 14 '18 at 9:37
JackFurby
asked Nov 13 '18 at 16:19
JackFurbyJackFurby
113
113
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
The command I ran wasrspec
. This called on a rake file which is included above. The point that error occurred wascompany.save!
. I have added the stacktrace above
– JackFurby
Nov 14 '18 at 9:33
add a comment |
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
The command I ran wasrspec
. This called on a rake file which is included above. The point that error occurred wascompany.save!
. I have added the stacktrace above
– JackFurby
Nov 14 '18 at 9:33
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
The command I ran was
rspec
. This called on a rake file which is included above. The point that error occurred was company.save!
. I have added the stacktrace above– JackFurby
Nov 14 '18 at 9:33
The command I ran was
rspec
. This called on a rake file which is included above. The point that error occurred was company.save!
. I have added the stacktrace above– JackFurby
Nov 14 '18 at 9:33
add a comment |
1 Answer
1
active
oldest
votes
How to properly do model testing with ActiveStorage in rails?
Using the info from there and adapting to your specifics, try the following factory:
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
EDIT:
You may also need to add this to your config/environments/test.rb file:
config.active_storage.service = :test
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285233%2fvalidation-failed-logo-cant-be-blank-when-logo-uploaded-using-rspec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
How to properly do model testing with ActiveStorage in rails?
Using the info from there and adapting to your specifics, try the following factory:
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
EDIT:
You may also need to add this to your config/environments/test.rb file:
config.active_storage.service = :test
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
add a comment |
How to properly do model testing with ActiveStorage in rails?
Using the info from there and adapting to your specifics, try the following factory:
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
EDIT:
You may also need to add this to your config/environments/test.rb file:
config.active_storage.service = :test
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
add a comment |
How to properly do model testing with ActiveStorage in rails?
Using the info from there and adapting to your specifics, try the following factory:
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
EDIT:
You may also need to add this to your config/environments/test.rb file:
config.active_storage.service = :test
How to properly do model testing with ActiveStorage in rails?
Using the info from there and adapting to your specifics, try the following factory:
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
EDIT:
You may also need to add this to your config/environments/test.rb file:
config.active_storage.service = :test
answered Nov 13 '18 at 16:22
MarkMark
1,9861723
1,9861723
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
add a comment |
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
Thank you for replying. Unfortunately, I think this will not work in my situation (I tried it and it just caused the same error when creating the company). One requirement in the model is for a logo to be present. If there is no fix I have tried to remove this requirement which words but ideally fixing the issue is preferred.
– JackFurby
Nov 13 '18 at 17:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285233%2fvalidation-failed-logo-cant-be-blank-when-logo-uploaded-using-rspec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where exactly does that error occur? Please provide the command you ran and the error message including the stacktrace.
– spickermann
Nov 13 '18 at 17:55
The command I ran was
rspec
. This called on a rake file which is included above. The point that error occurred wascompany.save!
. I have added the stacktrace above– JackFurby
Nov 14 '18 at 9:33