Skip to content

Commit 0eb074d

Browse files
committed
Add book model testing
1 parent d6da9f0 commit 0eb074d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

spec/factories/books.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FactoryBot.define do
2+
factory :book do
3+
title { Faker::Book.title }
4+
author { Faker::Book.author }
5+
description { Faker::Lorem.paragraph }
6+
association :recommender, factory: :user
7+
end
8+
end

spec/models/book_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'rails_helper'
2+
3+
describe Book, type: :model do
4+
let(:book) { create(:book) }
5+
6+
it 'is expected to have a valid factory' do
7+
expect(book.valid?).to eq true
8+
end
9+
10+
context 'Associations' do
11+
it { should belong_to(:recommender).class_name('User') }
12+
end
13+
14+
context 'Validations' do
15+
it { should validate_presence_of(:recommender) }
16+
it { should validate_presence_of(:title) }
17+
it { should validate_presence_of(:author) }
18+
it { should validate_presence_of(:description) }
19+
20+
it 'is invalid without a title' do
21+
book.title = nil
22+
expect(book.valid?).to eq false
23+
end
24+
25+
it 'is invalid without a recommender' do
26+
book.recommender = nil
27+
expect(book.valid?).to eq false
28+
end
29+
30+
it "is not valid with a empty title" do
31+
book.title = ''
32+
expect(book).to_not be_valid
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)