ruby - How do I return false in RSpec as the expected result when checking if directory exist?
问题描述
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?
解决方案
Use to_not
. Try
expect(Pathname(File.dirname(logfile))).to_not be_directory
https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
推荐阅读
- c++ - 为什么编译器不喜欢与“Iter”关联的所有字符串?
- sql - SQL WITH 语句
- rust - 如何从不同的模块导入两个具有相同名称的类型?
- javascript - 冲突:由 setWebhook 请求 Telegraf (Telegram Bot) 和 MenuBuilder (Node js) 终止
- google-chrome-devtools - 有没有办法检查伪代码内容的字符实体?
- javascript - 需要通过强制布局平移/缩放到 ECharts 图上节点的动态位置
- fabricjs - 如何获得旋转组的实际边界?
- typescript - 使用接口定义交集,而不是类型
- php - 在base64中裁剪图像边距
- macos - ddev 无法启动或重新启动,并出现“docker Read timed out”