と同じ方法を期待:RSpecのを許可し、異なる引数:: Service.run 2回、その中
module Temp
class Service
def self.do_job
# first call step 1
run("step1", {"arg1"=> "v1", "arg2"=>"v2"})
# second call step 2
run("step2", {"arg3"=> "v3"})
end
def self.run(name, p)
# do smth
return true
end
end
end
私は2回目の呼び出しに提供される引数をテストしたいですメソッド:最初の引数 'step2'を使って実行する 同じメソッドの最初の呼び出しを無視したいが、最初の引数 'step1'を実行する。
私はRSpecのテストRSpec.describe "My spec", :type => :request do
describe 'method' do
it 'should call' do
# skip this
allow(Temp::Service).to receive(:run).with('step1', anything).and_return(true)
# check this
expect(Temp::Service).to receive(:run) do |name, p|
expect(name).to eq 'step2'
# check p
expect(p['arg3']).not_to be_nil
end
# do the job
Temp::Service.do_job
end
end
end
持っているが、私はどのように正確に同じ方法を可能に使用し、期待するエラー
expected: "step2"
got: "step1"
(compared using ==)
を得ましたか。
期待に欠けているように思えるので。単に 'expect(Temp :: Service).to(' run ').with(' step2 '、 "arg3" => "v3"){true} 'を使用してください。 – fanta
2番目の引数より複雑なチェックを行いたい。だから私はブロックで作った。 このように ['arg3'、 'arg4']。each do | arg_good | expect(p [arg_good])。not_to be_nil end' –