Created
May 8, 2018 13:52
-
-
Save shinkufencer/2cdf26489792666b892c3407db6c6d17 to your computer and use it in GitHub Desktop.
exception後の挙動を確かめるrspec
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
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