Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model class specs #8

Merged
merged 2 commits into from
Nov 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Timecop here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ale tutaj nie potrzeba Timecop :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A, no to olej :)

On 24 Nov 2015, at 16:09, Wojciech Korzeniowski [email protected] wrote:

In spec/models/desk_spec.rb #8 (comment):

  • end
  • describe 'owner' do
  • it 'is required' do
  •  expect(build(:desk, owner: nil)).to_not be_valid
    
  • end
  • end
  • describe 'finish_date' do
  • it 'is presence' do
  •  expect(build(:desk, finish_at: nil)).to_not be_valid
    
  • end
  • it 'finish date has to be after start date' do
  •  desk = build(:desk)
    
  •  desk.finish_at = 1.day.ago(desk.start_at)
    
    Ale tutaj nie potrzeba Timecop :(


Reply to this email directly or view it on GitHub https://github.com/elpassion/ideashare/pull/8/files#r45746688.

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