Skip to content

Commit

Permalink
test added: response
Browse files Browse the repository at this point in the history
  • Loading branch information
angdev committed Oct 30, 2014
1 parent cc674f8 commit 4d3e937
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
16 changes: 12 additions & 4 deletions app/models/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 37 additions & 1 deletion spec/unit/models/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4d3e937

Please sign in to comment.