在rspec和测试方面相对较新,正在尝试为此代码编写测试
@assessments = Assessment.where(assigned_user_id: current_user.id)
到目前为止,这是我尝试过的方法,但不确定是否正确,我被告知控制器规范不建议这样做,因此我尝试使用单元测试来实现它。
spec_file
require 'rails_helper'
# Test suite for the assessment model
RSpec.describe Assessment, type: :model do
let(:admin) { User.new(email: 'rockcoolsaint@example.com', password: 'password', password_confirmation: 'password', role: :admin) }
let(:behavioral_specialist) { User.new(email: 'bs@example.com', password: 'password', password_confirmation: 'password', role: :behavioral_specialist) }
let(:behavioral_specialist_2) { User.new(email: 'bs2@example.com', password: 'password', password_confirmation: 'password', role: :behavioral_specialist) }
let(:bs_user_assessments) { Assessment.where(assigned_user_id: behavioral_specialist.id) }
describe 'Validations' do
it { should validate_presence_of(:title) }
it 'is valid with all attributes' do
expect(build(:assessment, user: admin, assigned_user: behavioral_specialist)).to be_valid
end
it 'is valid if assigned_user_id equals behavioral_specialist_id' do
bs_user_assessments.each do |assessment|
expect(assessment.assigned_user_id).to eq(behavioral_specialist.id)
end
end
it 'is valid if assigned_user_id does not equal behavioral_specialist_2_id' do
bs_user_assessments.each do |assessment|
expect(assessment.assigned_user_id).to_not eq(behavioral_specialist_2.id)
end
end
end