Skip to content

Commit

Permalink
Merge pull request #8 from elpassion/feature-model-specs
Browse files Browse the repository at this point in the history
Model class specs
  • Loading branch information
WojciechKo committed Nov 25, 2015
2 parents 648eb0f + da97210 commit e432cd1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
65 changes: 64 additions & 1 deletion spec/models/desk_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
require 'rails_helper'

RSpec.describe Desk, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:desk) { build(:desk) }
let(:user) { build(:user) }

it 'factory is valid' do
expect(desk).to be_valid
end

describe 'owner' do
it 'is required' do
desk.owner = nil
expect(desk).not_to be_valid
end
end

describe 'finish_date' do
it 'is presence' do
desk.finish_at = nil
expect(desk).not_to be_valid
end

it 'finish date has to be after start date' do
desk.finish_at = 1.day.ago(desk.start_at)
expect(desk).not_to be_valid
end

it 'finish date cannot be in the past' do
desk.finish_at = 1.hour.ago(DateTime.current)
expect(desk).not_to be_valid
end
end

describe 'start_date' do
it 'is presence' do
desk.start_at = nil
expect(desk).not_to be_valid
end
end

describe 'owner' do
let(:idea) {
build(:idea, owner: user, desk: desk)
}

before do
desk.save
idea.save
end

it 'has only one idea by specific user in desk' do
user.save

second_idea = Idea.create(owner: user, desk: desk, name: 'New user idea')
second_idea.save

expect { second_idea.save }.not_to change { desk.ideas.count }
expect(desk.ideas.first.name).to eq idea.name
expect(second_idea.errors).to include(:user)
end

it 'has multiple idea by different user in single' do
expect { create(:idea, desk: desk, name: 'First idea')
create(:idea, desk: desk, name: 'Other idea') }.to change { desk.ideas.count }.by(2)
end
end
end
35 changes: 35 additions & 0 deletions spec/models/idea_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rails_helper'

RSpec.describe Idea, type: :model do

it 'factory is valid' do
expect(build(:idea)).to be_valid
end

describe 'owner' do
it 'is assigned to user' do
expect(build(:idea, owner: nil)).not_to be_valid
end
end

describe 'assignation to desk' do
it 'is assigned to desk' do
expect(build(:idea, desk: nil)).not_to be_valid
end
end

describe 'name' do
it 'is required' do
expect(build(:idea, name: nil)).not_to be_valid
end
end

describe 'if time is up to add new idea' do
xit 'will not add idea to an expired desk' do
Timecop.freeze(2015, 10, 1, 11, 0)
finish_time = DateTime.new(2015, 10, 1, 14, 0)
desk = build(:desk, finish_at: finish_time)
expect(build(:idea, desk: desk)).not_to be_valid
end
end
end
28 changes: 28 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rails_helper'

RSpec.describe User, type: :model do

let (:owner) { create(:user) }

let (:other_user) { create(:user) }

let (:user_with_empty_date_of_birth) { build(:user, date_of_birth: nil)}

it 'factory is valid' do
expect(build(:user)).to be_valid
end

it 'assigns desks to user' do
expect { create(:desk, owner: owner)
create(:desk, owner: owner)
create(:desk, owner: other_user)
}.to change { owner.desks.count }.by(2)
end

describe 'date_of_birth' do
it 'is required' do
expect(user_with_empty_date_of_birth).to_not be_valid
expect(user_with_empty_date_of_birth.errors[:date_of_birth]).to_not be_empty
end
end
end

0 comments on commit e432cd1

Please sign in to comment.