/
post_spec.rb
85 lines (71 loc) · 2.56 KB
/
post_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'rails_helper'
RSpec.describe Post, type: :model do
describe "validation" do
it { is_expected.to validate_presence_of :content }
# We're getting round this by actually looping through the stack of candidates
# describe "slug validation" do
# before do
# create(:post, published_at: 1.day.ago, slug: "the-same-slug")
# end
# it "requires slugs to be unique for posts on the same day" do
# new_post = Post.create(content: "content", published_at: 1.day.ago, slug: "the_same_slug")
#
# expect(new_post.valid?).to eq false
# end
# end
end
describe "slug" do
context "when the Post has a slug" do
it "uses the slug" do
post = create(:post, title: "title", slug: "slug", content: "content")
expect(post.slug).to eq "slug"
end
it "cleans provided slugs" do
post = create(:post, slug: "this isn't going to work as a slug")
expect(post.slug).to eq "this-isnt-going-to-work-as-a-slug"
end
end
context "when the Post has a title" do
it "generates the slug from the title" do
post = create(:post, title: 'The Title')
expect(post.slug).to eq 'the-title'
end
end
context "when the Post doesn't have a title" do
it "generates the slug from the content" do
post = create(:post, title: nil, content: "This is the content")
expect(post.slug).to eq 'this-is-the-content'
end
it "truncates long" do
post = create(:post, content: Faker::Lorem.characters(500))
expect(post.slug.length).to eq 150
end
end
context "when the slug has changed" do
it "updates the slug" do
post = create(:post, slug: "slug")
post.update(slug: "slug-2")
expect(post.slug).to eq "slug-2"
end
it "cleans the slug" do
post = create(:post, slug: "slug")
post.update(slug: "slug 2")
expect(post.slug).to eq "slug-2"
end
end
context "when the slug hasn't changed, but other attributes have" do
it "does not update the slug" do
post = create(:post, slug: 'slug')
post.update(title: "A new title", content: "Some new content")
expect(post.slug).to eq "slug"
end
end
context "when the slug isn't unique for the day" do
it "tries the next candidate" do
old_post = create(:post, slug: 'slug', published_at: 2.days.ago)
post = create(:post, slug: 'slug', title: 'title', published_at: 2.days.ago)
expect(post.slug).to eq 'title'
end
end
end
end