From 4d3e937e05e1611133d5d00fb870d1884e3eceeb Mon Sep 17 00:00:00 2001 From: Hong ChulJu Date: Thu, 30 Oct 2014 16:15:22 +0900 Subject: [PATCH] test added: response --- app/models/response.rb | 16 +++++++++---- spec/unit/models/response_spec.rb | 38 ++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/app/models/response.rb b/app/models/response.rb index c51f7d8..5a4b54f 100644 --- a/app/models/response.rb +++ b/app/models/response.rb @@ -4,14 +4,22 @@ class Response < ActiveRecord::Base belongs_to :notice belongs_to :user - scope :time, -> (notice) { find_by_notice_id(notice.id).created_at.localtime.strftime("%Y-%m-%d %T") } + validates :status, presence: { message: "회답을 선택해주십시오." }, + inclusion: { in: STATUSES, message: "올바르지 않은 회답입니다." } scope :responsed_to_go, -> (notice) { notice.responses.where(status: "go") } + def self.time (notice) + response = find_by_notice_id(notice.id) + + if response + return response.created_at.localtime.strftime("%Y-%m-%d %T") + else + return "" + end + end + def responsed_at created_at.localtime.strftime("%Y-%m-%d %T") end - validates :status, presence: { message: "회답을 선택해주십시오." }, - inclusion: { in: STATUSES, message: "올바르지 않은 회답입니다." } - end \ No newline at end of file diff --git a/spec/unit/models/response_spec.rb b/spec/unit/models/response_spec.rb index de41aa8..5a1097c 100644 --- a/spec/unit/models/response_spec.rb +++ b/spec/unit/models/response_spec.rb @@ -4,11 +4,47 @@ it "should check validation" do expect(Response.new.save).to eq(false) expect(Response.new(status: "false", notice: FactoryGirl.create(:notice)).save).to eq(false) - + expect(Response.new(status: "yes", notice: FactoryGirl.create(:notice)).save).to eq(true) expect(Response.new(status: "maybe", notice: FactoryGirl.create(:notice)).save).to eq(true) expect(Response.new(status: "no", notice: FactoryGirl.create(:notice)).save).to eq(true) expect(Response.new(status: "go", notice: FactoryGirl.create(:notice)).save).to eq(true) expect(Response.new(status: "wait", notice: FactoryGirl.create(:notice)).save).to eq(true) end + + describe "#responsed_to_go" do + it "should return responsed to go" do + user1 = FactoryGirl.create(:user) + user2 = FactoryGirl.create(:user) + notice = FactoryGirl.create(:notice) + + response1 = Response.create!(user: user1, notice: notice, status: "maybe") + expect(Response.responsed_to_go(notice)).to be_empty + + response2 = Response.create!(user: user2, notice: notice, status: "go") + expect(Response.responsed_to_go(notice)).to contain_exactly(response2) + end + end + + describe "#responsed_at" do + it "should return created_at with localtime manner" do + user = FactoryGirl.create(:user) + notice = FactoryGirl.create(:notice) + + response = Response.create!(user: user, notice: notice, status: "maybe", created_at: Date.new(2014, 10, 30)) + expect(response.responsed_at).to eq("2014-10-30 00:00:00") + end + end + + describe ".time" do + it "should return responsed time to given notice" do + user = FactoryGirl.create(:user) + notice = FactoryGirl.create(:notice) + + expect(user.responses.time(notice)).to eq("") + + response = Response.create!(user: user, notice: notice, status: "maybe", created_at: Date.new(2014, 10, 30)) + expect(user.responses.time(notice)).to eq("2014-10-30 00:00:00") + end + end end \ No newline at end of file