|
20 | 20 | require "rails_helper"
|
21 | 21 |
|
22 | 22 | RSpec.describe Post do
|
| 23 | + let(:user) { create(:user) } |
| 24 | + |
23 | 25 | describe "validations" do
|
24 |
| - let(:post) { build(:post) } |
| 26 | + let(:post) { build(:post, user: user) } |
25 | 27 |
|
26 | 28 | it "requires a body" do
|
27 | 29 | post.body = " "
|
|
35 | 37 | end
|
36 | 38 |
|
37 | 39 | describe "scopes" do
|
38 |
| - let!(:post) { create(:post) } |
39 |
| - let!(:draft) { create(:draft) } |
| 40 | + let!(:post) { create(:post, user: user) } |
| 41 | + let!(:draft) { create(:draft, user: user) } |
40 | 42 |
|
41 | 43 | describe "published" do
|
42 | 44 | it "returns published posts" do
|
|
103 | 105 |
|
104 | 106 | describe "#publish" do
|
105 | 107 | it "sets published_at and saves the record" do
|
106 |
| - post = build(:draft) |
| 108 | + post = build(:draft, user: user) |
107 | 109 | post.publish
|
108 | 110 | expect(post.published_at).to be_present
|
109 | 111 | expect(post).to be_persisted
|
110 | 112 | end
|
111 | 113 |
|
112 | 114 | it "sets appropriate slug when there are multiple posts with the same title" do
|
113 |
| - post1 = build(:draft, plain: "My favorite music") |
| 115 | + post1 = build(:draft, plain: "My favorite music", user: user) |
114 | 116 | post1.publish
|
115 | 117 | expect(post1.slug).to eq('my-favorite-music')
|
116 | 118 |
|
117 |
| - post2 = build(:draft, plain: "My favorite music") |
| 119 | + post2 = build(:draft, plain: "My favorite music", user: user) |
118 | 120 | post2.publish
|
119 | 121 | expect(post2).to be_persisted
|
120 | 122 | expect(post2.slug).not_to eq("my-favorite-music")
|
121 | 123 | expect(post2.slug).to match(/my-favorite-music/)
|
122 | 124 | end
|
123 | 125 |
|
124 | 126 | it "returns falsly value when it fails validations" do
|
125 |
| - post = build(:draft, body: ' ') |
| 127 | + post = build(:draft, body: ' ', user: user) |
126 | 128 | expect(post.publish).to be_falsy
|
127 | 129 | end
|
128 | 130 | end
|
129 | 131 |
|
130 | 132 | describe "#save_as_draft" do
|
131 | 133 | it "sets published_at to nil and saves the record" do
|
132 |
| - post = build(:draft) |
| 134 | + post = build(:draft, user: user) |
133 | 135 | post.save_as_draft
|
134 | 136 | expect(post.published_at).to be_nil
|
135 | 137 | expect(post).to be_persisted
|
|
139 | 141 | describe "words and word_count" do
|
140 | 142 | let(:post) { build(:post,
|
141 | 143 | body: "{}",
|
142 |
| - plain: "This is five words long.") |
| 144 | + plain: "This is five words long." , |
| 145 | + user: user) |
143 | 146 | }
|
144 | 147 |
|
145 | 148 | it "returns an array of words" do
|
|
0 commit comments