TDD FOR RORLAB.

(2). Plaza / Post Model.
PR #57, #63, #67


#57 Plaza Model


  • Plaza Model 생성 

          COMMAND

Plaza Model 생성 

rails g model plaza postitable:references{polymorphic} visible:boolean 
Plaza Model DB 생성  rake db:migrate rake db:test:clone

#57 관계설정


models/plaza.rb

belongs_to :postitable, polymorphic: true


models/post.rb has_one :plaza, :as => :postitable, :dependent => :destroy
models/question.rb has_one :plaza, :as => :postitable, :dependent => :destroy

...
models/###.rbhas_one :plaza, :as => :postitable, :dependent => :destroy

#57 Spec 작성 (factory)

Plaza Factory 추가

- 다른 모델과의 관계 선언 및 객체 타입별 생성
factory :post_plaza, class: "plaza" do
  association :postitable, :factory => :post
  postitable_type 'post'
  visible false
end
Post Factory 변경 - Post 객체 생성시 Plaza 객체 추가 after(:create) do |post| post.plaza = FactoryGirl.create(:post_plaza, postitable_id: post.id, postitable_type: "post") end

#57 Spec 작성

Post, Question 생성시 자동으로 생성되는 Plaza 확인
describe Plaza do
describe "모델 객체의 생성" do
  it "> Post 객체를 유효한 데이터로 생성시 Plaza에 추가된다." do
    post = create(:post)

    expect(post.plaza).to be_valid
  end
  ...

end
end

#63 네이밍 관련 이슈

FactoryGirl.create(...) -> create(...)

post_plaza (x) -> plaza_post (o)

#67 Post Model


after_create callback method 추가
  • factory의 after(:create)을 model의 after_create 콜백 메소드로 교 체  
app/models/post.rb

after_create :set_plaza_post

def set_plaza_post
  self.create_plaza
end 

궁금한 점..

has_one 관계 검증?

#67 POST HIT 관련 SPEC 추 가


Spec을 먼저 작성!

  • spec/models/post_spec.rb  

it "> Post 생성시 hit의 기본값은 0이다." do
   expect(build(:post).hit).to eq(0)
end 


#67 Post migration 생성


$ rails g migration add_column_to_post published_at:datetime hit:integer

Post Model Column Hit 변경

db/migrate/add_column_to_post.rb

add_column :posts, :hit, :integer, default: 0 
Post Model DB Column 업데이트

$ rake db:migrate
$ rake db:test:clone

테스트 통과

#67 published_at 관련 spec 추가


Spec을 먼저 작성!

  • spec/models/post_spec.rb
it "> published가 false일때 published_at은 nil이다." do 
  post = create(:post, published: false)
  expect(post.published_at).to be_nil
end      
it "> published가 true일때 published_at에 Time이 입력된다." do 
  post = create(:post, published: true)
  expect(post.published_at).to be_a_kind_of(Time)
end    

#67 published_at 코드 추가


models/post.rb
  • published가 활성화시 현재시간을 넣고, 비활성화시 nil을 입력
before_save :set_published_at
private
def set_published_at
  if (published == true || published == "true" || published == "1") && published_at.nil?
    self.published_at = Time.now
  elsif (published != true && published != "true" && published != "1") && published_at
    self.published_at = nil
  end
end 

테스트 통과

#67 title, content spec 추가


Spec을 먼저 작성

it "> Title이 3글자 이상 255글자 이하이어야 한다." do
   should ensure_length_of(:title).is_at_least(3).is_at_most(255)
end
it "> Content가 0글자 이상 10000글자 이하이어야 한다." do
   should ensure_length_of(:content).is_at_least(0).is_at_most(10000)
end   

#67 title, content 코드 추가


  • title과 content의 최소, 최대 길이 유효성 검사
validates :title, presence: true, :length => { :minimum => 3, :maximum => 255 }
validates :content, presence: true, :length => { :minimum => 0, :maximum => 10000 } 



테스트 통과

TDD FOR RORLAB.

By hanguk lee

TDD FOR RORLAB.

한 주동안 진행했던 과정 공유

  • 1,239