polymorphic association에서 factory_gir을 사용하여 rspec 상에서 unit test를 작성하는데 어려움이 있습니다.
실험을 위해서 아주 기본 예제로 하였는데
기본예제에서도 왜인지 파악되지 않는 오류가 있어서 이렇게 도움을 청합니다...
# db/schema.rb
ActiveRecord::Schema.define(version: 20160412134106) do
create_table "cars", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "pictures", force: :cascade do |t|
t.integer "imageable_id"
t.string "imageable_type"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
schema가 위와 같이 되어있고,
아주 기본적인 polymorphic association을 하였습니다.
# app/models/picture.rb
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
# app/models/car.rb
class Car < ActiveRecord::Base
has_many :pictures, as: :imageable
end
위와 같이 rails polymorphic 예제와 동일하게 구성해보았습니다.
그리고 factory girl과 rspec를 이용해서 test를 아래와 같이 작성하였는데요...
# spec/factories/pictures.rb
FactoryGirl.define do
factory :car_picture do
association :imageable, factory: :car
end
end
# spec/factories/cars.rb
FactoryGirl.define do
factory :car do
name "MyString"
end
end
# spec/models/car_spec.rb
require 'rails_helper'
RSpec.describe Car, type: :model do
let(:car_picture) { build(:car_pictur) }
it { expect(car_picture).to validate_uniqueness_of(:imageable_type).scoped_to(:imageable_id) }
end
rspec 명령을 수행하면
$ bundle exec rspec -b
F
Failures:
1) Car
Failure/Error: let(:car_picture) { build(:car_picture) }
NameError:
uninitialized constant CarPicture
...
Finished in 0.00241 seconds (files took 1.4 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/car_spec.rb:5 # Car
$
위와 같이 uninitialized constant CarPicture
라는 오류가 나는데 왜 나는지 모르겠네요...
추가 질문
댓글에서 언급한 오류를 질문용 프로젝트에도 재현하였습니다.
# app/models/picture.rb
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
validates :imageable_type, uniqueness: { scope: :imageable_id } # 추가된 행!!!!!!!
validates :imageable, presence: true # 추가된 행!!!!!!!
end
다른 코드들은 답변으로 제시해주신 factory명과 해당 클래스명이 다를때 사용하는 class: Picture
를 추가하였고, 나머지는 전부 같고 위 picture.rb에서 마지막쯤에 validates :imageable, presence: true # 추가된 행!!!!!!!
을 추가하고 test를 돌리니
아래와 같은 오류가 발생합니다.(댓글에서도 언급한..)
$ bundle exec rspec
F
Failures:
1) Car should validate that :imageable_type is case-sensitively unique within the scope of :imageable_id
Failure/Error: it { expect(car_picture).to validate_uniqueness_of(:imageable_type).scoped_to(:imageable_id) }
NameError:
wrong constant name cAR
# ./spec/models/car_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.03813 seconds (files took 1.39 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/car_spec.rb:5 # Car should validate that :imageable_type is case-sensitively unique within the scope of :imageable_id
$
spec/factories/pictures.rb
에서
FactoryGirl.define do
factory :car_picture, class: Picture do
association :imageable, factory: :user
end
end
car대신 user로 바꾸니
$ bundle exec rspec
F
Failures:
1) Car should validate that :imageable_type is case-sensitively unique within the scope of :imageable_id
Failure/Error: it { expect(car_picture).to validate_uniqueness_of(:imageable_type).scoped_to(:imageable_id) }
NameError:
wrong constant name uSER
# ./spec/models/car_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.04852 seconds (files took 2.14 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/car_spec.rb:5 # Car should validate that :imageable_type is case-sensitively unique within the scope of :imageable_id
$
위와 같이 wrong constant name uSER
과 같이 모델이름에서 첫문자만 소문자이고 나머지는 전부 대문자로 되는현상이 보였고, 모델이름이 UserFriend라면 uSERfRIEND
로 오류가 나느 현상이 보여졌습니다...
뭐가 문제일까요?