Skip to content

Instantly share code, notes, and snippets.

@shinkufencer
Created May 8, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinkufencer/2cdf26489792666b892c3407db6c6d17 to your computer and use it in GitHub Desktop.
Save shinkufencer/2cdf26489792666b892c3407db6c6d17 to your computer and use it in GitHub Desktop.
exception後の挙動を確かめるrspec
require "rspec"
### target class
class Calculate
def initialize
# わかりやすい初期値をいれる
@result = -9999
end
# 10を引数で割る
def ten_division(number)
@result = 10 / number
@result
end
def last_result
@result
end
end
### spec
describe ".ten_division" do
context "10を指定した場合" do
let(:obj) { Calculate.new }
subject { obj.ten_division(2) }
it "last_resultで1が戻ってくる" do
is_expected.to eq(5)
end
end
context "0を指定した場合" do
let(:obj) { Calculate.new }
subject { obj.ten_division(0) }
it "ZeroDivisionErrorがあがる" do
expect { subject }.to raise_error(ZeroDivisionError)
end
it "last_resultで-9999が戻ってくる" do
begin
subject
rescue
end
expect(obj.last_result).to eq(-9999)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment