spec/parole/commentable_spec.rb in parole-0.1.1 vs spec/parole/commentable_spec.rb in parole-0.1.2

- old
+ new

@@ -1,107 +1,41 @@ require 'spec_helper' describe Parole::Commentable do - describe :comments do - before do - spawn_comment_model - spawn_commenter_model 'User' - spawn_commentable_model 'Article' + describe 'general `comments` relation' do + let(:relation_class) { ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Comment } - run_migration do - create_table(:articles, force: true) - create_table(:users, force: true) - end - end + context 'without defined roles' do + before do + spawn_comment_model + spawn_commentable_model 'Article' - let(:commenter) { User.create } - let(:commentable) { Article.create } - - context 'without role attribute' do - let(:comment) { commentable.comments.create(commenter: commenter, comment: 'Booya') } - - it { expect(comment).to be_persisted } - it { expect(comment.comment).to eql 'Booya' } - it { expect(comment.commenter).to eql commenter } - it { expect(comment.commentable).to eql commentable } - end - - context 'with role attribute' do - let(:comment) { commentable.comments.create(role: 'YEP', commenter: commenter, comment: 'Booya') } - it { expect(comment).to_not be_persisted } - it { expect(comment.errors.full_messages).to eql ['Role is invalid'] } - end - end - - describe 'role comments' do - before do - spawn_comment_model - spawn_commenter_model 'User' - spawn_commentable_model 'Article' do - acts_as_commentable roles: [:photos, :videos] + run_migration do + create_table(:articles, force: true) + end end - run_migration do - create_table(:articles, force: true) - create_table(:users, force: true) - end + it { expect(Article.create.comments).to be_instance_of(ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Comment) } + it { expect(Article.create.comments.new.role).to be_blank } end - let(:commenter) { User.create } - let(:commentable) { Article.create } + context 'with roles' do + before do + spawn_comment_model + spawn_commentable_model 'Article' do + acts_as_commentable roles: [:photos, :videos] + end - context 'with commentable role association method' do - let(:comment) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') } - - it { expect(comment).to be_persisted } - it { expect(comment.role).to eql 'photos' } - it { expect(comment.comment).to eql 'Booya' } - it { expect(comment.commenter).to eql commenter } - it { expect(comment.commentable).to eql commentable } - end - - context 'with commentable main association method' do - context 'with valid role' do - let(:comment) { commentable.comments.create(role: 'photos', commenter: commenter, comment: 'Booya') } - - it { expect(comment).to be_persisted } - it { expect(comment.role).to eql 'photos' } - it { expect(comment.comment).to eql 'Booya' } - it { expect(comment.commenter).to eql commenter } - it { expect(comment.commentable).to eql commentable } - end - - context 'with invalid role' do - let(:comment) { commentable.comments.create(role: 'NOPE', commenter: commenter, comment: 'Booya') } - it { expect(comment).to_not be_persisted } - it { expect(comment.errors.full_messages).to eql ['Role is invalid'] } - end - end - end - - describe 'cache counters' do - before do - spawn_comment_model - spawn_commenter_model 'User' - spawn_commentable_model 'Article' do - acts_as_commentable roles: [:photos, :videos] - end - - run_migration do - create_table(:users, force: true) - create_table(:articles, force: true) do |t| - t.integer :photos_comments_count, default: 0 - t.integer :videos_comments_count, default: 0 - t.integer :comments_count, default: 0 + run_migration do + create_table(:articles, force: true) end end - end - let(:commenter) { User.create } - let(:commentable) { Article.create } - let(:create_comment!) { commentable.photos_comments.create(commenter: commenter, comment: 'Booya') } - - it { expect { create_comment! }.to change { commentable.reload.photos_comments_count }.from(0).to(1) } - it { expect { create_comment! }.to_not change { commentable.reload.videos_comments_count } } - it { expect { create_comment! }.to change { commentable.reload.comments_count }.from(0).to(1) } + it { expect(Article.create.comments).to be_instance_of(relation_class) } + it { expect(Article.create.photos_comments).to be_instance_of(relation_class) } + it { expect(Article.create.videos_comments).to be_instance_of(relation_class) } + it { expect(Article.create.comments.new.role).to be_blank } + it { expect(Article.create.photos_comments.new.role).to eql 'photos' } + it { expect(Article.create.videos_comments.new.role).to eql 'videos' } + end end end