Skip to content

Commit

Permalink
Merge pull request #2247 from eduardoj/tests_for_model_comment
Browse files Browse the repository at this point in the history
[webui][ci] Initial tests for model Comment
  • Loading branch information
bgeuken committed Nov 14, 2016
2 parents cd31d99 + 26454b9 commit d906c10
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/spec/factories/comment_packages.rb
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :comment_package do
body { Faker::Lorem.paragraph }
user
package
end
end
7 changes: 7 additions & 0 deletions src/api/spec/factories/comment_requests.rb
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :comment_request do
body { Faker::Lorem.paragraph }
user
bs_request
end
end
97 changes: 97 additions & 0 deletions src/api/spec/models/comment_spec.rb
@@ -0,0 +1,97 @@
require "rails_helper"

RSpec.shared_examples "a comment" do
let(:comment_factory) { described_class.name.underscore }
let(:comment) { create(comment_factory) }

describe "A comment" do
it { expect(build(comment_factory)).to be_valid }
end

describe "Comment associations" do
it { is_expected.to belong_to(:bs_request).inverse_of(:comments) }
it { is_expected.to belong_to(:project).inverse_of(:comments) }
it { is_expected.to belong_to(:package).inverse_of(:comments) }
it { is_expected.to belong_to(:user).inverse_of(:comments) }

it { is_expected.to have_many(:children).dependent(:destroy).class_name('Comment').with_foreign_key('parent_id') }
end

describe "Comment validations" do
it { is_expected.to validate_presence_of(:body) }
it { is_expected.to validate_presence_of(:type) }
it { is_expected.to validate_presence_of(:user) }
end

describe "to_xml" do
let(:builder) { Nokogiri::XML::Builder.new }
let(:comment_element) { builder.doc.css('comment') }

context "without parent" do
before do
comment.to_xml(builder)
end

it "creates xml with correct attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment.id.to_s)
expect(comment_element.attribute('when').value.to_datetime).to eq(comment.created_at)
expect(comment_element.attribute('who').value).to eq(comment.user.login)

expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end

context "with parent" do
before do
parent_comment = create(comment_factory)
comment.parent_id = parent_comment.id
comment.to_xml(builder)
end

it "creates xml with correct attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment.id.to_s)
expect(comment_element.attribute('when').value.to_datetime).to eq(comment.created_at)
expect(comment_element.attribute('who').value).to eq(comment.user.login)
expect(comment_element.attribute('parent').value).to eq(comment.parent_id.to_s)

expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end
end

describe "blank_or_destroy" do
context "without children" do
before do
comment
end

it 'should be destroyed' do
expect { comment.blank_or_destroy }.to change { Comment.count }.by(-1)
end
end

context "with children" do
before do
create(comment_factory, parent: comment)
end

it "shouldn't be destroyed" do
expect { comment.blank_or_destroy }.to_not change { Comment.count }
expect(comment.body).to eq 'This comment has been deleted'
expect(comment.user.login).to eq '_nobody_'
end
end
end
end

RSpec.describe CommentPackage do
it_behaves_like "a comment"
end

RSpec.describe CommentProject do
it_behaves_like "a comment"
end

RSpec.describe CommentRequest do
it_behaves_like "a comment"
end

0 comments on commit d906c10

Please sign in to comment.