Skip to content

Commit 0be7cb8

Browse files
committed
Add factory bot and refine the models specs with Factory bot
1 parent 6026088 commit 0be7cb8

20 files changed

+186
-83
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ group :development, :test do
3535
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
3636
gem 'database_cleaner'
3737
gem 'factory_bot_rails'
38-
gem 'faker', '1.9.1'
38+
gem 'faker'
3939
gem 'pry'
4040
gem 'rubocop', '~> 0.76.0', require: false
4141
gem 'rubocop-performance'

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ GEM
107107
factory_bot_rails (5.1.1)
108108
factory_bot (~> 5.1.0)
109109
railties (>= 4.2.0)
110-
faker (1.9.1)
111-
i18n (>= 0.7)
110+
faker (2.7.0)
111+
i18n (>= 1.6, < 1.8)
112112
ffi (1.11.3)
113113
font-awesome-rails (4.7.0.5)
114114
railties (>= 3.2, < 6.1)
@@ -338,7 +338,7 @@ DEPENDENCIES
338338
database_cleaner
339339
devise
340340
factory_bot_rails
341-
faker (= 1.9.1)
341+
faker
342342
font-awesome-rails
343343
jbuilder (~> 2.5)
344344
listen (>= 3.0.5, < 3.2)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This is my RoR full stack web development learning project & example, including
1717
- [Slim Templating 4](http://slim-lang.com/), HTML templating engine.
1818
- [Devise](https://github.com/plataformatec/devise), flexible authentication solution.
1919
- [RSpec Rails 3](https://relishapp.com/rspec/rspec-rails/v/3-9/docs), BDD and test suite.
20+
- [Faker](https://github.com/faker-ruby/faker), A library for generating fake data such as names, addresses, and phone numbers.
21+
- [Factory Bot Rails](https://github.com/thoughtbot/factory_bot_rails), a library for setting up Ruby objects as test data.
2022
- [Bulma CSS](https://bulma.io/), mobile first CSS framework.
2123
- [Postgresql 10](https://www.postgresql.org/)
2224
- [Peru](https://github.com/buildinspace/peru), a package manager for including other people's code in your projects.

db/schema.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
# This file is auto-generated from the current state of the database. Instead
2-
# of editing this file, please use the migrations feature of Active Record to
3-
# incrementally modify your database, and then regenerate this schema definition.
4-
#
5-
# Note that this schema.rb definition is the authoritative source for your
6-
# database schema. If you need to create the application database on another
7-
# system, you should be using db:schema:load, not running all the migrations
8-
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
9-
# you'll amass, the slower it'll run and the greater likelihood for issues).
10-
#
11-
# It's strongly recommended that you check this file into your version control system.
12-
131
ActiveRecord::Schema.define(version: 2019_11_28_134418) do
142

153
# These are extensions that must be enabled in order to support this database

spec/factories/category.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :category do
3+
name { Faker::Book.unique.genre }
4+
end
5+
end

spec/factories/post.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FactoryBot.define do
2+
factory :post do
3+
fake_title = Faker::Book.unique.title
4+
title { fake_title }
5+
content { "## #{fake_title}"}
6+
release { false }
7+
category
8+
user
9+
end
10+
end

spec/factories/tag.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :tag do
3+
name { Faker::Book.unique.genre }
4+
end
5+
end

spec/factories/user.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FactoryBot.define do
2+
factory :user do
3+
email { Faker::Internet.unique.email }
4+
display_name { Faker::Name.name }
5+
password { 'password' }
6+
password_confirmation { 'password' }
7+
end
8+
9+
trait :locked do
10+
locked_at { DateTime.now }
11+
end
12+
13+
trait :without_display_name do
14+
display_name { nil }
15+
end
16+
end

spec/models/category_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
require 'spec_helper'
2+
require 'rails_helper'
23

34
describe Category do
45
it 'should has default columns' do
56
is_expected.to have_db_column(:name).of_type(:string)
67
end
8+
9+
describe '#paper_trail' do
10+
context 'with has_paper_trail attribute' do
11+
let(:dummy_data) { create :category }
12+
it 'will has association with the versions table and has data' do
13+
expect(dummy_data.versions.present?).to eq true
14+
end
15+
end
16+
end
717
end

spec/models/post_spec.rb

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'rails_helper'
23

34
describe Post do
45
it 'should has default columns' do
@@ -12,41 +13,46 @@
1213

1314
it 'should has indexes' do
1415
is_expected.to have_db_index [:category_id]
16+
is_expected.to have_db_index [:user_id]
17+
end
18+
19+
describe '#paper_trail' do
20+
context 'with has_paper_trail attribute' do
21+
let(:dummy_data) { create :post }
22+
it 'will has association with the versions table and has data' do
23+
expect(dummy_data.versions.present?).to eq true
24+
end
25+
end
1526
end
1627

1728
describe '#permalink' do
18-
describe 'given valid title' do
29+
context 'given valid title' do
30+
let(:dummy_post) { create :post, title: 'Ruby is Awesome' }
1931
it 'will generate valid permalink' do
20-
post = Post.new title: 'Ruby is Awesome', content: 'testing.'
21-
expect(post.permalink).to eq 'ruby_is_awesome'
32+
expect(dummy_post.permalink).to eq 'ruby_is_awesome'
2233
end
2334
end
2435

25-
describe 'given blank between title' do
36+
context 'given blank between title' do
37+
let(:dummy_post) { create :post, title: ' Ruby is Awesome ' }
2638
it 'will strip blank char and generate valid permalink' do
27-
post = Post.new title: ' Ruby is Awesome ', content: 'testing.'
28-
expect(post.permalink).to eq 'ruby_is_awesome'
39+
expect(dummy_post.permalink).to eq 'ruby_is_awesome'
2940
end
3041
end
42+
end
3143

32-
describe 'given valid title and generated permalink' do
33-
let(:category) { Category.create name: 'Dumb'}
34-
let(:user) { User.create email: '[email protected]', password: 'abc', password_confirmation: 'abc' }
35-
36-
after do
37-
post = Post.last
38-
post.destroy
39-
category.destroy
40-
user.destroy
44+
describe '#content_to_html' do
45+
context 'given valid not empty content' do
46+
let(:dummy_post) { create :post, content: '### Hello' }
47+
it 'will generate valid html' do
48+
expect(dummy_post.content_to_html).to include '<h3>Hello</h3>'
4149
end
50+
end
4251

43-
it 'will save the permalink into database correctly' do
44-
post = Post.new title: 'Dummy title 123', content: 'testing', category: category, user: user
45-
post.save
46-
generated_permalink = 'dummy_title_123'
47-
expect(post.permalink).to eq generated_permalink
48-
current_data = Post.where(permalink: generated_permalink)
49-
expect(current_data.present?).to eq true
52+
context 'given content with html tags' do
53+
let(:dummy_post) { create :post, content: '<div>hello</div>' }
54+
it 'will omit the html tags and wrap it as paragraph' do
55+
expect(dummy_post.content_to_html).to include '<p>hello</p>'
5056
end
5157
end
5258
end

0 commit comments

Comments
 (0)