Skip to content

Commit

Permalink
write some specs for models
Browse files Browse the repository at this point in the history
  • Loading branch information
satococoa committed Feb 5, 2012
1 parent f1e5026 commit 096fde9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/models/report.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Report < ActiveRecord::Base
belongs_to :user
has_many :comments
has_many :comments, :dependent => :destroy

validates :date, :presence => true
validates :body, :presence => true
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class User < ActiveRecord::Base
has_many :reports
has_many :reports, :dependent => :destroy
has_many :comments, :dependent => :destroy

validates :uid, :presence => true
validates :nickname, :presence => true
Expand Down
4 changes: 2 additions & 2 deletions spec/fabricators/comment_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Fabricator(:comment) do
report_id 1
user_id 1
report!
user!
body "MyText"
end
2 changes: 1 addition & 1 deletion spec/fabricators/report_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Fabricator(:report) do
date "2012-01-31"
user_id 1
user!
body "MyText"
end
31 changes: 30 additions & 1 deletion spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# coding: utf-8
require 'spec_helper'

describe Comment do
pending "add some examples to (or delete) #{__FILE__}"
describe '.create' do
let(:comment) { Fabricate :comment }
subject { comment }
context 'パラメータが正しい時' do
it { should be_valid }
end
context '本文が空の時' do
before do
comment.body = ''
end
it { should_not be_valid }
end
end
describe '#destroy' do
let(:comment) { Fabricate :comment }
subject { Comment.where(comment.id).first }
context '日報が削除された時' do
before do
comment.report.destroy
end
it { should be_nil }
end
context 'ユーザーが削除された時' do
before do
comment.user.destroy
end
it { should be_nil }
end
end
end
46 changes: 45 additions & 1 deletion spec/models/report_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
# coding: utf-8
require 'spec_helper'

describe Report do
pending "add some examples to (or delete) #{__FILE__}"
describe '.create' do
let(:report) { Fabricate :report }
subject { report }
context 'パラメータが正しいとき' do
it { should be_valid }
end
context '本文が空の時' do
before do
report.body = ''
end
it { should_not be_valid }
end
context '日付が空の時' do
before do
report.date = nil
end
it { should_not be_valid }
end
end
describe '.latest_date' do
subject { Report.latest_date }
before do
Fabricate(:report, :date => '2012-01-01')
end
context '2012-01-01 の日報だけがあるとき' do
it { should == Date.parse('2012-01-01') }
end
context 'さらに 2012-01-02 の日報があるとき' do
before do
Fabricate(:report, :date => '2012-01-02')
end
it { should == Date.parse('2012-01-02') }
end
end
describe '#destroy' do
let(:report) { Fabricate :report }
subject { Report.where(report.id).first }
context 'ユーザーが削除された時' do
before do
report.user.destroy
end
it { should be_nil }
end
end
end

0 comments on commit 096fde9

Please sign in to comment.