I'm new to testing Rails so this is probably a very small thing but I can't figure out what's wrong. So I have some models I'd like to test. Right now the tests are simple; testing the presence of attributes and saving if all validations are met.
One of my models Profile belongs_to my Users model and passes all these tests spec/models/profiles_spec.rb:
require 'rails_helper'
RSpec.describe Profile, type: :model do
context 'validation tests' do
it 'ensures user_id presence' do
profile = Profile.new(platform: 0, region: 0, tag: 'GamerTag', sr: 1600).save
expect(profile).to eq(false)
end
it 'ensures platform presence' do
profile = Profile.new(user_id: 1, region: 0, tag: 'GamerTag', sr: 1600).save
expect(profile).to eq(false)
end
it 'ensures region presence' do
profile = Profile.new(user_id: 1, platform: 0, tag: 'GamerTag', sr: 1600).save
expect(profile).to eq(false)
end
it 'ensures tag presence' do
profile = Profile.new(user_id: 1, platform: 0, region: 0, sr: 1600).save
expect(profile).to eq(false)
end
it 'ensures sr presence' do
profile = Profile.new(user_id: 1, platform: 0, region: 0, tag: 'GamerTag').save
expect(profile).to eq(false)
end
it 'should save successfully' do
profile = Profile.new(user_id: 1, platform: 0, region: 0, tag: 'GamerTag', sr: 1600).save
expect(profile).to eq(true)
end
end
end
app/models/profile.rb:
class Profile < ApplicationRecord
validates :platform, presence: true
validates :region, presence: true
validates :tag, presence: true
validates :sr, presence:true
belongs_to :user
enum platform: [:pc, :xbl, :psn]
enum region: [:us, :eu]
end
But then there are my other models, which "pass" all the attribute presence validation tests, which theres something wrong there because they still pass when I comment out their attribute validations and fail the 'should save successfully' test.
The most confusing part? When I run the rails console and manually test it returns the expected value (true), like with my Student model which belongs_to :profile.
So I really have no idea what's going on here. Any ideas please throw them out. If you all need any more information, let me know.
development.sqlite3,test.sqlite3andschema.rb, runrails db:migrateand thenrspec, theprofile_spec.rbstill passes all tests. This is Rails 5.2 - Gregory Jarosprofile_spec.rb'sshould save successfullytest pass and the other model's versions of the test don't? They all have the same basic tests asprofile_spec.rb, they all have foreign keys in the schema andbelongs_toassociations in their models. There is SOMETHING different aboutProfilebut I don't understand what it is. Unless I'm misunderstanding you I still don't see what's causing this. - Gregory Jaros