2012-07-31 3 views
1

모델에서 SFTP를 스텁 아웃하려고합니다. 여기RSpec : SFTP - 스텁 방식의 개인 메서드

class BatchTask 
    require 'net/sftp' 

    def get_file_stream(host, username, password, path_to_dir, filename) 
    raise ArgumentError if host.nil? or username.nil? or password.nil? or path_to_dir.nil? or filename.nil? 
    file_stream = nil 
    Net::SFTP.start(host, username, password) do |sftp| 
     sftp.dir.glob(path_to_dir, filename) do |entry| 
     # Verify the directory contents 
     raise RuntimeError(true), "file: #{path_to_dir}/#{filename} not found on SFTP server" if entry.nil? 
     file_stream = sftp.file.open("#{path_to_dir}/#{entry.name}") 
     end 
    end 
    file_stream 
    end 

end 

사양은 다음과 같습니다 : 여기

require 'spec_helper' 

describe "SftpToServer" do 
    let(:ftp) { BatchTask::SftpToServer.new } 

it "should return a file stream" do 
    @sftp_mock = mock('sftp') 
    @entry = File.stubs(:reads).with("filename").returns(@file) 
    @entry_mock = mock('entry') 
    @entry_mock.stub(:name).and_return("filename") 
    @sftp_mock.stub_chain(:dir, :glob).and_yield(@entry_mock) 
    Net::SFTP.stub(:start).and_yield(@sftp_mock) 
    @sftp_mock.stub_chain(:file, :open).with("filename").and_yield(@file) 

    ftp.get_file_stream("ftp.test.com", "user", "password", "some/pathname", "filename").should be_kind_of(IO) 
    end 

end 

는 스택 트레이스 것 : 여기 모델이다 내가이 검색 좀했습니다

NoMethodError in 'SftpToServer should return a file stream' 
private method `open' called for #<Object:0x10c572620> 
/Users/app/models/batch_task/sftp_to_server.rb:12:in `get_file_stream' 
/Users/app/models/batch_task/sftp_to_server.rb:9:in `get_file_stream' 
/Users/app/models/batch_task/sftp_to_server.rb:8:in `get_file_stream' 
./spec/models/batch_task/sftp_to_server_spec.rb:15: 

하지만, RSpec에가 치료 왜 나는 알아낼 수 없습니다 개인 메서드로 sftp.file.open ...

미리 아이디어를 주셔서 감사합니다!

답변

1

그래서 무슨 일이 일어 났는지 생각해 냈습니다. open() 메서드를 @sftp_mock 개체의 일부로 설명하지 않았으므로 @ sftp.file.open은 개인 메서드처럼 보입니다.

@sftp_mock.stub!(:file) { @sftp_mock } 
@sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file) 

나는 또한 @file 선언을 실종됐다가 ... 전체 사양 파일은 다음과 같습니다이 같이 open() 메서드에 체인 수 있도록이 해결하는 것은, sftp_mock.file 반환 @sftp_mock @ 데 필요한 :

require 'spec_helper' 

describe "SftpToServer" do 
    let(:ftp) { BatchTask::SftpToServer.new } 

it "should return a file stream" do 
    @file = File.new("#{RAILS_ROOT}/spec/files/express_checkout_tdr.csv", "r") 
    @sftp_mock = mock('sftp') 
    @entry = File.stubs(:reads).with("filename").returns(@file) 
    @entry_mock = mock('entry') 
    @entry_mock.stub(:name).and_return("filename") 
    @sftp_mock.stub_chain(:dir, :glob).and_yield(@entry_mock) 
    Net::SFTP.stub(:start).and_yield(@sftp_mock) 
    @sftp_mock.stub!(:file) { @sftp_mock } 
    @sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file) 

    ftp.get_file_stream("ftp.test.com", "user", "password", "some/pathname", "filename").should be_kind_of(IO) 
    end 

end 

이 다른 유래 질문이 개념으로 나를 도와 :

Stub chain together with should_receive는 또한, 조롱과 SFTP를 스텁에 더 좋은 방법이 있는지 의견을 주시기 바랍니다. 이 스펙 예제는 다소 추한 것입니다!

관련 문제