0

I am writing an RSpec test to check if a given path is a directory. In once scenario this isn't the case and I expect a false, but I don't know how to raise this.

So my test looks like this:

context 'when a logfile is given' do
    it 'does not contain a folder first' do
      logfile = '/tmp/qwertyasd/random/path/rspec.log'
      expect(Pathname(File.dirname(logfile))).to be_directory
      # I want this expect to return false. I cannot use !be_directory
      # I know I should not use be_directory, I was thinking about something like !be_directory but this raise an error.
    end

     it 'create a folder for it' do
       logfile = '/tmp/qwertyasd/random/path/rspec.log'
       Pathname.new(logfile).parent.descend { |path| FileUtils.mkdir path if path.parent.writable? && !path.exist? }
       expect(Pathname(File.dirname(logfile))).to be_directory
       `rm -rf /tmp/qwertyasd`
     end
end

I have tried using !be_directory but this gives me an error. Any tips?

4

2 回答 2

2

Use to_not. Try

expect(Pathname(File.dirname(logfile))).to_not be_directory

https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

于 2019-04-19T11:32:04.690 回答
1

Looks like you need smth like:

expect(Pathname(File.dirname(logfile))).not_to be_directory
于 2019-04-19T11:31:58.290 回答