Skip to content

Commit

Permalink
Test and implement CreateComment::CheckProhibitedWords service
Browse files Browse the repository at this point in the history
  • Loading branch information
adamniedzielski committed Dec 27, 2014
1 parent 5691b47 commit a35d48b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/services/create_comment/check_prohibited_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateComment
class CheckProhibitedWords
PROHIBITED_WORDS = ['bad', 'cheap', 'boring']

def self.build
new
end

def call(title)
count = (title.to_s.split & PROHIBITED_WORDS).size
raise ProhibitedWordError if count > 1
end

class ProhibitedWordError < StandardError; end
end
end
19 changes: 19 additions & 0 deletions spec/services/create_comment/check_prohibited_words_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

describe CreateComment::CheckProhibitedWords do

before do
@service = CreateComment::CheckProhibitedWords.build
end

it "raises exception if title contains at least two prohibited words" do
title = "bad boring movie"
expect { @service.call(title) }.
to raise_error(CreateComment::CheckProhibitedWords::ProhibitedWordError)
end

it "does not raise if title contains one prohibited word" do
title = "cheap but interesting movie"
expect { @service.call(title) }.not_to raise_error
end
end

0 comments on commit a35d48b

Please sign in to comment.