creddit/spec/models/post_spec.rb

89 lines
1.8 KiB
Ruby
Raw Normal View History

2015-07-14 16:29:22 -04:00
require 'rails_helper'
describe Post, type: :model do
let(:post) { build(:post) }
it { should belong_to(:user) }
it { should belong_to(:subcreddit) }
2015-07-16 11:57:27 -04:00
it { should have_many(:comments) }
2015-07-14 16:29:22 -04:00
2015-07-20 15:12:26 -04:00
it { should delegate_method(:username).to(:user).with_prefix }
2015-12-11 15:26:51 -05:00
it { should delegate_method(:slug).to(:subcreddit).with_prefix }
2015-07-20 15:12:26 -04:00
2015-07-16 11:57:27 -04:00
context 'when adding a comment' do
let(:post) { create(:post) }
it 'should update the cache_counter for comments' do
expect do
create(:comment, post: post)
end.to change { post.comments_count }.by(1)
end
end
2015-07-14 16:29:22 -04:00
context 'with valid data' do
it 'should be valid' do
expect(post).to be_valid
end
it 'should allow blank content' do
post.content = ''
expect(post).to be_valid
end
it 'should allow a blank link' do
post.link = ''
expect(post).to be_valid
end
end
context 'with invalid data' do
it 'should not allow a blank title' do
post.title = ''
expect(post).to be_invalid
end
it 'should not allow a long title' do
post.title = 'a' * 301
expect(post).to be_invalid
end
it 'should not allow long content' do
post.content = 'a' * 15001
expect(post).to be_invalid
end
end
context '#to_param' do
it 'generates the correct param' do
post.save
expect(post.to_param).to eq("#{post.id}-#{post.title.parameterize}")
end
end
2015-07-16 11:57:27 -04:00
context '#comments?' do
let(:post) { create(:post) }
context 'with comments' do
before(:each) do
create(:comment, post: post)
end
it 'should respond with true' do
expect(post.comments?).to be(true)
end
end
context 'without comments' do
it 'should respond with false' do
expect(post.comments?).to be(false)
end
end
end
2015-07-14 16:29:22 -04:00
end