Programming

RSpec : 메서드가 호출되었는지 테스트하는 방법은 무엇입니까?

procodes 2020. 8. 15. 14:02
반응형

RSpec : 메서드가 호출되었는지 테스트하는 방법은 무엇입니까?


RSpec 테스트를 작성할 때 테스트 실행 중에 메서드가 호출되었는지 확인하기 위해 다음과 같은 코드를 많이 작성했습니다 (인수를 위해 상태를 조사 할 수 없다고 가정 해 보겠습니다. 메서드가 수행하는 작업이 효과를보기가 쉽지 않기 때문에 호출 후 개체의

describe "#foo"
  it "should call 'bar' with appropriate arguments" do
    called_bar = false
    subject.stub(:bar).with("an argument I want") { called_bar = true }
    subject.foo
    expect(called_bar).to be_true
  end
end

내가 알고 싶은 것은 : 이것보다 더 좋은 구문이 있습니까? 위의 코드를 몇 줄로 줄일 수있는 펑키 한 RSpec 굉장함을 놓치고 있습니까? should_receive이 작업을 수행해야하는 것처럼 들리지만 더 읽어 보면 정확히 수행하는 작업이 아닌 것 같습니다.


it "should call 'bar' with appropriate arguments" do
  expect(subject).to receive(:bar).with("an argument I want")
  subject.foo
end

rspec expect구문에서 이것은 다음과 같습니다.

expect(subject).to receive(:bar).with("an argument I want")

아래가 작동합니다

describe "#foo"
  it "should call 'bar' with appropriate arguments" do
     subject.stub(:bar)
     subject.foo
     expect(subject).to have_received(:bar).with("Invalid number of arguments")
  end
end

문서 : https://github.com/rspec/rspec-mocks#expecting-arguments

참고 URL : https://stackoverflow.com/questions/21262309/rspec-how-to-test-if-a-method-was-called

반응형