Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Directory exist with Rspec

I'm testing lib/pdf_helper.rb. So i create spec/lib directory. Then I create a file pdf_helper_spec.rb in spec/lib directory. As I'm testing that pdf folder should be in public folder and here is my code

require 'spec_helper'
require 'pdf_helper'

    describe "Pdfhelpers" do
        it "Should be in public folder" do
        file = File.new ("#{Rails.root}/public/pdf")
        if File.exist?(file) == 'true'
            puts "Success"
        else
            puts"failed"
        end


    end
  end

Am i right?? I'm new on RSpec.

like image 386
user1563221 Avatar asked Sep 15 '12 07:09

user1563221


3 Answers

expect(File).not_to exist("#{Rails.root}/public/pdf")

Will work for both files and folders.

like image 173
elhoyos Avatar answered Nov 20 '22 13:11

elhoyos


Pathname also turns out to be fairly readable for this sort of thing.

require 'pathname'

# ...

expect(Pathname.new('file.txt')).to exist
expect(Pathname.new('file.txt')).to be_file
expect(Pathname.new('dir')).to be_directory
like image 10
Hakanai Avatar answered Nov 20 '22 14:11

Hakanai


If you're wanting to know if a file is a directory, then you can use the File.directory? function.

like image 4
FluffyJack Avatar answered Nov 20 '22 12:11

FluffyJack