🖥

#Ruby Rspec expect with keyword args example

に公開
require "rspec"

class Foo
  def self.run(x:, y:)
    Bar.run(x: x, y: y)
  end
end

class Bar
  def self.run(x:, y:)
    false
  end
end

describe do
  subject { Foo.run(x: "X", y: "Y") }

  describe do
    before do
      expect(Bar).to receive(:run).with(x: "X", y: "Y").and_return true
    end

    it { is_expected.to be true }
  end

  describe do
    let(:param) { { x: "X", y: "Y" } }

    before do
      expect(Bar).to receive(:run).with(**param).and_return true
    end

    it { is_expected.to be true }
  end

  describe do
    let(:param) { { "x" => "X", "y" => "Y" } }

    before do
      expect(Bar).to receive(:run).with(x: "X", y: "Y").and_return true
    end

    it { is_expected.to be true }
  end

end

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3116

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-05-02

Discussion