2012-07-31 14 views
6

Sto tentando di eliminare Net :: SFTP da un oggetto. Ecco il modello:RSpec: Stubing 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 

Ecco le specifiche:

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).with("filename").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 

Ecco lo stacktrace:

Spec::Mocks::MockExpectationError in 'SftpToServer should return a file stream' 
Mock "entry" received :name with unexpected arguments 
    expected: ("filename") 
     got: (no args) 
/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: 

Prima di tutto, è il mio approccio proprio qui? Voglio rimuovere la funzionalità di SFTP poiché possiamo essere certi che è abbastanza ben collaudato. Invece, voglio concentrarmi sull'assicurare che un flusso di file venga restituito dalla "scatola nera" all'interno di get_file_stream().

In secondo luogo, come posso eseguire correttamente lo stub out sftp.file.open() per ottenere ciò?

Grazie in anticipo per qualsiasi idea qui!

risposta

7

In primo luogo beffardo fuori SFTP è una buona idea per due motivi:

  1. Non stai scrivendo un test sftp, concentrarsi sui test proprio quello che si imposta fuori a test.
  2. Si rimuove qualsiasi dipendenza dalla rete nei test: non si desidera che i test inizino a non riuscire a causa di qualcosa fuori dal proprio controllo.

Per quanto riguarda l'errore va, questo è il tuo problema immediato:

@entry_mock.stub(:name).with("filename").and_return("filename") 

Qui si sta spegnendo entry.name("filename") piuttosto che solo entry.name.

modificarla in:

@entry_mock.stub(:name).and_return("filename") 

e fatemi sapere come si ottiene.

+0

@cheesewasel - ha funzionato come un fascino! Grazie per l'aiuto! – Sly

+0

Nessun problema. :) –