외부 서비스 호출을 통해 정적 변수를 시작하는 정적 메서드가 있습니다. 클래스 변수를 초기화하는 동안 외부 서비스 호출이 만들어지지 않도록 정적 메서드 호출을 스텁 (stub)하고 싶습니다. 여기에 간단한 코드로 예제가 나와 있습니다.rspec을 통해 정적 클래스 메서드를 스텁하는 방법
class ABC
def self.ini
return someCallToMyExternalLibrary # i don't want the execution to go there while testing
end
@@config = self.ini
def method1
return @@config['download_URL']
end
end
이제 정적 개체 호출을 내 개체로 스텁하여 @@ config가 원하는 응답으로 초기화되도록하고 싶습니다. 여러 가지를 시도해 본 결과 @@ config가 내 객체로 초기화되지 않았지만 구현 된 호출에 의해서만 초기화 된 것처럼 보입니다.
describe ABC do
let(:myObject) { Util.jsonFromFile("/data/app_config.json")}
let(:ABC_instance) { ABC.new }
before(:each) do
ABC.stub(:ini).and_return(myObject)
end
it "check the download url" do
ABC_instance.method1.should eql("download_url_test")
# this test fails as @@config is not getting initialized with my object
# it returns the download url as per the implementation.
end
end
심지어 클래스 변수가 실행이 도달 할 때 초기화됩니다, 그러나 그것은 또한 도움이되지 않았다 전에 처음 실행될 생각과 spec_helper에 stubing 노력했다. 나는 이것을 잠시 동안 붙들고있다. 누군가 제발 구세주가 되십시오.
으로 업그레이드를 시도해 볼 수 있습니다. 피터, 나는 또한 어려운 길을 알아 냈다. – manyu