Skip to content

Instantly share code, notes, and snippets.

@kuredev
Last active November 16, 2020 14:53
Show Gist options
  • Save kuredev/42f5e1790285a757c5bc3faa3811dbc4 to your computer and use it in GitHub Desktop.
Save kuredev/42f5e1790285a757c5bc3faa3811dbc4 to your computer and use it in GitHub Desktop.
クラスのモックを作る時(Aws::EC2::Resource を例に)
require "aws-sdk"
RSpec.describe "Test" do
it "test" do
allow(Aws::EC2::Resource).to receive(:new).and_return("Aws1") # ①
allow(Aws::EC2::Resource).to receive(:new).with(region: "ap-northeast-1").and_return("Aws2") # ②
allow(Aws::EC2::Resource).to receive(:new).with(hash_including(region: "ap-northeast-2")).and_return("Aws3") # ③
ec2 = Aws::EC2::Resource.new
pp ec2 # => "Aws1", 引数が無いので①に該当
ec2 = Aws::EC2::Resource.new(region: "us-east-1")
pp ec2 # => "Aws1", 引数がどれにも合致しないので①に該当
ec2 = Aws::EC2::Resource.new(region: "ap-northeast-1")
pp ec2 # => "Aws2", 引数が完全に合致するので②に該当
ec2 = Aws::EC2::Resource.new(stub_responses: true, region: "ap-northeast-1")
pp ec2 # => "Aws1", 引数がどれにも合致しないので①に該当、②は `hash_including` ではなく完全一致でないとハマらない
ec2 = Aws::EC2::Resource.new(region: "ap-northeast-2")
pp ec2 # => "Aws3", 引数が③に完全合致
ec2 = Aws::EC2::Resource.new(stub_responses: true, region: "ap-northeast-2")
pp ec2 # => "Aws3", 引数が③に合致、 余計なstub_responses があるけど hash_including だから合致される
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment