1
콜백 before_save 모델 호출하지 않는 경우 - 예를 들어, 선행 및 후행 공백 스트립 하나레일 RSpec에/공장 봇 내가 before_save 콜백의 번호와 사용자 모델이
응용 프로그램/모델/user.rb를 :
def strip_whitespace_in_user_names
self.first_name.strip!
self.first_name.gsub!(" ", "")
self.last_name.strip!
self.last_name.gsub!(" ", "")
end
기본 모델 사양이 있습니다. 실제로이 기능이 작동하는지 확인하고 싶습니다. 여기
RSpec.describe User, type: :model do
let(:user) { build :poorly_defined_user }
it "has no leading white space" do
expect(user.first_name).not_to end_with(" ")
end
end
가 poorly_defined_user의 공장 정의입니다 : 예를 들어, "나단은" "나단"
사양/모델/user_spec.rb 반환해야합니다, 그러나
require 'faker'
password = Faker::Internet.password
# Factory to define a user
FactoryBot.define do
factory :poorly_defined_user, class: User do
first_name " asd "
last_name "AS DF "
handle "BLASDF824"
email Faker::Internet.email
password password
password_confirmation password
end
end
을 때 테스트를 실행하면이 기대가 실패합니다. 나는 postman (API 용)을 점검하고 콜백이 올바르게 실행되고 사용자의 속성이 올바르게 설정되었습니다.
왜 이런 일이 발생했는지, 또는 Rspec/Factory Bot이 실제로 작동하는 방식을 반영하여 테스트를 재구성하는 방법에 대한 도움이 필요합니다.
완벽하게 작동합니다. 도움에 감사드립니다. –