Given the model set up like this:
class Course < ApplicationRecord
belongs_to :faculty
has_many :teachings
has_many :faculty, through: :teachings
validates :name, uniqueness: { scope: [:faculty_id, :period,
:semester, :year] }
end
class Faculty < ApplicationRecord
has_secure_password
has_many :courses
has_many :courses, :through => :teachings
validates :email, { presence: true, uniqueness: true }
validates :first_name, presence: true
validates :last_name, presence: true
validates :password, presence: true
end
I am trying to test the creation of course like so:
RSpec.describe Course, :type => :model do
it "is valid with when period and faculty are unique" do
course = create(:course)
expect(course).to be_invalid
end
end
I get the following error when I run the test:
1) Course is valid with when period and faculty are unique Failure/Error: course = create(:course)
ActiveRecord::RecordInvalid: Validation failed: Faculty must exist
I have tried creating a faculty, and using that in the creation of the course, yet still know luck.
I have looked up how to deal with factory bot and relations and tried some of that, but I'm just too ignorant on testing to get it to work. I'm hoping to get some insight specific to my situation right now.
Aucun commentaire:
Enregistrer un commentaire