-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test and implement CreateComment::CheckProhibitedWords service
- Loading branch information
1 parent
5691b47
commit a35d48b
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
spec/services/create_comment/check_prohibited_words_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |