ruby '2.6.5' rails '6.0.0'
medicine_spec.rb
before do
@medicine = FactoryBot.build(:medicine)
@medicine.image = fixture_file_upload('public/images/money.jpg')
end
describe 'New registration of medicine' do
context 'When new drug registration is successful' do
it "medicine,symptom,date,You can register if the image exists" do
expect(@medicine).to be_valid
end
end
This test code gives the following error.
I made a hypothesis.
Since I got got errors: User must exist
, I hypothesized that user information is related.
medicine.rb
class Medicine < ApplicationRecord
has_one_attached :image
belongs_to :user
with_options presence: true do
validates :medicine
validates :symptom
validates :date
end
end
user.rb
has_many :medicines
The model association is assembled. The next thing I thought about was hypothesis that the association was not formed at the time of the test to enter the value.
medicines.rb
FactoryBot.define do
factory :medicine do
medicine { 'Loxonin' }
symptom { 'Cold' }
date { Time.now.utc }
user
end
end
Added ʻassociation: user`. I will test it.
I passed the test safely!
I will pass the test without fail, but if an error occurs, I can check the connection of the application, so I will continue learning!
Recommended Posts