# [Ruby] Write the test by implementing the story of Nabeats of the world with ruby

#### Why did you decide

• I wanted to write a unit test with rspec
• Simple program was good
• The story of Nabeats of the world is simple, lean and interesting
• I came up with it
• I wanted to do it

#### Specifications

• First of all, “I will do something interesting, that is, Omoro.”
• Count from 1 to 40
• It becomes stupid when the number is 3
• Stupid when multiple of 3
• Become a dog when it is a multiple of 5 (if you are a multiple of 3 and 5, become a dog)
• Finally, “Omoro”

#### `omoro.rb`

``````
TSUJO_KAO_MOJI = "(･`ω･´)" # Normal
AHO_KAO_MOJI = "ʅ( ՞ਊ ՞)ʃ≡" # Aho's intention
DOG_KAO_MOJI = "∪･ω･∪" # Innu’s intention

puts "I'm going to do interesting things, that is, omoro."

(1..40).each do |figure|
sleep 0.5

if figure %5 == 0
puts figure.to_s + DOG_KAO_MOJI
next
end

if figure %3 == 0
puts figure.to_s + AHO_KAO_MOJI
next
end

if /3/ =~ figure.to_s
puts figure.to_s + AHO_KAO_MOJI
next
end

puts figure.to_s + TSUJO_KAO_MOJI
end

puts'Omorrow'
``````

Run with `ruby omoro.rb`

``` I’m going to do interesting things, that is, omoro 1 (・```ω・´) 2 (・```ω・´) 3ʅ( ՞ਊ՞)ʃ≡ 4 (・```ω・´) 5∪・ω・∪ 6ʅ( ՞ਊ՞)ʃ≡ 7 (・```ω・´) 8 (・```ω・´) 9ʅ( ՞ਊ՞)ʃ≡ 10∪・ω・∪ 11 (・```ω・´) 12ʅ( ՞ਊ՞)ʃ≡ 13ʅ( ՞ਊ՞)ʃ≡ 14 (・```ω・´) 15∪・ω・∪ 16 (・```ω・´) 17 (・```ω・´) 18ʅ( ՞ਊ՞)ʃ≡ 19 (・```ω・´) 20∪・ω・∪ 21ʅ( ՞ਊ՞)ʃ≡ 22 (・```ω・´) 23ʅ( ՞ਊ՞)ʃ≡ 24ʅ( ՞ਊ՞)ʃ≡ 25∪・ω・∪ 26 (・```ω・´) 27ʅ( ՞ਊ՞)ʃ≡ 28 (・```ω・´) 29 (・```ω・´) 30∪・ω・∪ 31ʅ( ՞ਊ՞)ʃ≡ 32ʅ( ՞ਊ՞)ʃ≡ 33ʅ( ՞ਊ՞)ʃ≡ 34ʅ( ՞ਊ՞)ʃ≡ 35∪・ω・∪ 36ʅ( ՞ਊ՞)ʃ≡ 37ʅ( ՞ਊ՞)ʃ≡ 38ʅ( ՞ਊ՞)ʃ≡ 39ʅ( ՞ਊ ՞)ʃ≡ 40∪・ω・∪ Omorrow

``````
did it.
However, with this, there is no method and all the processing is the same, so unit tests cannot be written.
The methods were divided by responsibility.

#### **`omoro_class.rb`**
```ruby

class Omoro
TSUKAMI = "I'm going to do some interesting things, that is, omoro."
OCHI = "Omoro"
TSUJO_KAO_MOJI = "(･`ω･´)"
AHO_KAO_MOJI = "ʅ( ՞ਊ ՞)ʃ≡"
DOG_KAO_MOJI = "∪･ω･∪"

#News
def main(max_kazu = 40)
speak (TSUKAMI)

(1..max_kazu).each do |figure|
sleep 1
kao_moji = verification(figure)
serif = make_serif(figure, kao_moji)
speak(serif)
end

speak(OCHI)
end

# Make lines
def make_serif(figure, kao_moji)
figure.to_s + kao_moji
end

# to decide
def verification(figure)
if figure %5 == 0
return DOG_KAO_MOJI
end

if figure %3 == 0 || /3/ =~ figure.to_s
return AHO_KAO_MOJI
end
TSUJO_KAO_MOJI
end

# Speak
def speak(serif)
puts serif
end
end

omoro = Omoro.new
omoro.main(40)
``````

Omorrow class is completed. As an aside, by passing an arbitrary number to the main method, you can dynamically change the Max number to be counted. If you enter 400, you will be a stupid or dog up to 300-399. It’s hard to do with human power, but it’s easy to write in a program.

Write the test. studying. I’m not using let because I don’t understand it. If you have a gentle Masakari, please write an example in the comments.

#### `omoro_spec.rb`

``````
require'rspec'
require_relative'../omoro_class'

RSpec.describe Omoro do
TSUJO_KAO_MOJI = "(･`ω･´)"
AHO_KAO_MOJI = "ʅ( ՞ਊ ՞)ʃ≡"
DOG_KAO_MOJI = "∪･ω･∪"

it'Generate lines of number (1) + emoticon ((・`ω・´))' do
expect(Omoro.new.make_serif(1, TSUJO_KAO_MOJI)).to eq '1(･`ω･´)'
end

it'Generate lines of number (3) + emoticon (3ʅ( ՞ਊ ՞)ʃ≡)' do
expect(Omoro.new.make_serif(3, AHO_KAO_MOJI)).to eq '3ʅ( ՞ਊ՞)ʃ≡'
end

it'inu' do
expect(Omoro.new.make_serif(5, DOG_KAO_MOJI)).to eq '5∪・ω・∪'
end

it '3 is attached' do
expect(Omoro.new.make_serif(33,'ʅ( ՞ਊ՞)ʃ≡')).to eq '33ʅ( ՞ਊ՞)ʃ≡'
end

it'verification' do
omoro = Omoro.new
(1..40).each do |figure|
if figure %5 == 0
expect(omoro.verification(figure)).to eq DOG_KAO_MOJI
elsif figure %3 == 0 || /3/ =~ figure.to_s
expect(omoro.verification(figure)).to eq AHO_KAO_MOJI
else
expect(omoro.verification(figure)).to eq TSUJO_KAO_MOJI
end
end
end

it'main' do
omoro = Omoro.new
expect {omoro.main(5) }.to output(
"I will do something interesting from now on, that is, omoro \n1(･`ω･´)\n2(･`ω･´)\n3ʅ( ՞ਊ՞)ʃ≡\n4(･`ω･´)\n5∪ ･Ω･∪\nOmoro\n").to_stdout
end
end
``````

I personally feel that the verification method and main method could be improved, but I didn’t know how to do it.

#### References

Katsu Sando - Wikipedia

[Ruby] Unit test starting with Rspec (Unit test) | qs Developers

Test standard output with Rspec - Qiita

Tags:

Updated: