Skip to content

Commit f9eb3b3

Browse files
committed
Add coverage for post model
1 parent 6c6a941 commit f9eb3b3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe BetterTogether::Post, type: :model do
6+
it_behaves_like 'an authorable model'
7+
8+
it 'has a valid factory' do
9+
expect(build(:better_together_post)).to be_valid
10+
end
11+
12+
it 'validates presence of title and content' do
13+
post = build(:better_together_post, title: nil, content: nil)
14+
expect(post).not_to be_valid
15+
expect(post.errors[:title]).to include("can't be blank")
16+
expect(post.errors[:content]).to include("can't be blank")
17+
end
18+
19+
describe '#to_s' do
20+
it 'returns the title' do
21+
post = build(:better_together_post, title: 'Example')
22+
expect(post.to_s).to eq 'Example'
23+
end
24+
end
25+
26+
describe 'after_create #add_creator_as_author' do
27+
it 'creates an authorship for the creator_id' do
28+
creator = create(:better_together_person)
29+
post = build(:better_together_post)
30+
# Ensure no prebuilt authorships from the factory
31+
post.authorships.clear
32+
post.creator_id = creator.id
33+
post.save!
34+
35+
expect(post.authors.reload.map(&:id)).to include(creator.id)
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)