Skip to content

Instantly share code, notes, and snippets.

@z-ohnami
Created September 11, 2014 06:30
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 z-ohnami/c81c6e97a11ef05af2e4 to your computer and use it in GitHub Desktop.
Save z-ohnami/c81c6e97a11ef05af2e4 to your computer and use it in GitHub Desktop.
Rspec basic sample
require "calc"
RSpec.describe Calc do
# before do
# @calc = Calc.new
# end
subject(:calc) {
#message expectation if not called -> test fail
logger = double('logger')
expect(logger).to receive(:log)
calc = Calc.new(logger)
}
context "when calc mode" do
it "given 2 and 3, return 5" do
expect(calc.add(2,3)).to eq(5)
end
it "given 5 and 8, return 13" do
expect(calc.add(5,8)).to eq(13)
# expect(calc.add(5,8)).not_to eq(5)
# expect(calc.add(5,8)).to be true
# expect(calc.add(5,8)).to be false
# expect(calc.add(5,8)).to be > 10
# expect(calc.add(5,8)).to be_between(1,10).inclusive
# expect(calc.add(5,8)).to respond_to(:add)
# expect(calc.add(5,8).integer?).to be true
# expect(calc.add(5,8)).to be_integer
end
end
context "tax calcurate" do
# let
# exampleごとに結果がキャッシュされる。example内で一度も呼び出されない場合は評価されない
# 遅延評価を回避したい場合は let! で定義しておく
let(:price) { 100 }
it { expect(calc.price(price,0.05)).to eq(105) }
it { expect(calc.price(price,0.08)).to eq(108) }
end
#stub test
context "add with user name" do
it {
user = double('user')
allow(user).to receive(:name).and_return('ohnami')
expect(calc.addWithName(5,2,user.name)).to eq('7 by ohnami')
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment