在检查目录是否存在时,如何将 RSpec 中的 return false 作为预期结果?

How do I return false in RSpec as the expected result when checking if directory exist?

我正在编写 RSpec 测试来检查给定路径是否为目录。在一次情况下情况并非如此,我预计会出现错误,但我不知道如何提出这个问题。

所以我的测试是这样的:

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

我试过使用 !be_directory 但这给了我一个错误。有什么建议吗?

看起来你需要这样的东西:

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

使用to_not。尝试

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

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