Skip to content

Instantly share code, notes, and snippets.

@halllllll
Created August 17, 2018 02:39
Show Gist options
  • Save halllllll/a65595693c7cb8352cacc21c2930bb9b to your computer and use it in GitHub Desktop.
Save halllllll/a65595693c7cb8352cacc21c2930bb9b to your computer and use it in GitHub Desktop.
RSpec practice

これを学習したメモっつかパクリです

class Calculation
def add(a, b)
a+b
end
def proportion(a, b)
a+(a*b)
end
end
require 'calculation'
# RSpec.describe '計算するやつ' do
RSpec.describe Calculation do
# before節 -- example(itブロック)の前に必ず呼ばれる初期化処理
# 最上位のdescribe直下のbeforeで定義したやつはその下のdescribeの中のexampleでも使用可能
before do
@calculation = Calculation.new
end
# before以外にexampleで使う変数を定義するものとしてletとsubjectがある
# subjectはitブロック外で使うようなやつ @つける必要がない
# letはテストケース上の一時変数みたいなもんでそんな重要じゃないときに使う context/describeブロック内で定義
subject(:calc){Calculation.new}
context "正常処理" do
it "1+1=2 ? " do
#calculation = Calculation.new
# expect -> テストの最小単位
expect(@calculation.add(1, 1)).to eq 2
end
it "2+5=7 ? " do
#calculation = Calculation.new
# expect(@calculation.add(2, 5)).to eq 7
expect(calc.add(2, 5)).to eq 7
end
end
context "異常処理" do
it '' do
end
end
# it(example/specify)のあとの引数を省略すると自動的に説明が付加される
it do
expect(@calculation.add(1, 3)).to eq 4
end
# pending: ブロックを記述しない場合に発生
it '1-1=0?'
# mathcer
describe 'mather test' do
it 'should not equal' do
# expect(@calculation.add(2, 100)).not_to eq 29
expect(calc.add(2, 100)).not_to eq 29
end
it 'should be positive number' do
expect(@calculation.add(19, -13)).to be >0
end
it 'should be a number between' do
expect(@calculation.add(1,10)).to be_between(1, 14)
end
it 'should have "add" method' do
# expect(@calculation).to respond_to :add
expect(calc).to respond_to :add
end
it 'should be interger number' do
expect(@calculation.add(29, 53).integer?).to be true
end
end
context '5%' do
let(:rate){0.05}
it do
expect(calc.proportion(100, rate)).to eq 105
end
end
context '8%' do
let(:rate){0.08}
it do
expect(calc.proportion(100, rate)).to eq 108
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment