require 'spec_helper' describe FBO::RemoteFile do let(:date) { Date.parse("2013-03-31") } let(:filename) { FBO::RemoteFile.filename_for_date(date) } let(:tmp_dir) { File.join(File.dirname(__FILE__), "..", "fixtures") } let(:ftp) { mock("Net::FTP", login: nil, getbinaryfile: nil, close: nil) } before do Net::FTP.stub(:new).and_return(ftp) end describe "class methods" do describe "for_date" do it "should raise an error if no date is given" do expect { FBO::RemoteFile.for_date(nil) }.to raise_error end it "should return an instance of FBO::RemoteFile" do remote_file = FBO::RemoteFile.for_date(date, tmp_dir: tmp_dir) remote_file.should be_an FBO::RemoteFile end end end context "everything is great" do subject { FBO::RemoteFile.new(filename, tmp_dir: tmp_dir) } it "should attempt to fetch the file via FTP" do ftp.should_receive(:login).ordered ftp.should_receive(:getbinaryfile).ordered ftp.should_receive(:close).ordered subject end it "should be possible to open the file and read its contents" do subject.readline.should_not be_nil end end context "temporary directory doesn't exist" do subject { FBO::RemoteFile.new(filename, tmp_dir: "foo/bar") } before do Dir.stub(:exists?).and_return(false) Dir.stub(:mkdir) File.stub(:new) end it "should create the temporary directory" do Dir.should_receive(:mkdir).with("foo/bar") subject end end end