存根File.exists?在 RSpec
Stubbing File.exists? in RSpec
我想测试以下 ruby 代码的 else
分支:
SCHEMA_SOURCE = if File.exist? SCHEMA_FILENAME
SCHEMA_FILENAME
else
ContentfulApi::Http
end
我找不到模拟文件不存在的方法 SCHEMA_FILENAME
。下面是我试过的简化版测试,报错:
it 'fails to find file when it does not exist' do
allow(File).to receive(:exists?).and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
it 'fails to find file when it does not exist' do
allow(File).to receive(:exists?).with(ContentfulApi::SCHEMA_FILENAME)\
.and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
这些都因错误而失败
expected: falsey value
got: true
it 'fails to find file when it does not exist' do
File.stub(:exists?) { false }
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
it 'fails to find file when it does not exist' do
File.stub!(:exists?).with(ContentfulApi::SCHEMA_FILENAME)\
.and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
这些都因错误而失败
undefined method `stub' for File:Class
我的前两次尝试遵循 Method Stubs 文档中的示例,尽管 类 和该文档中未探讨的对象之间可能存在区别。 (虽然 类 是 Ruby 中的对象,对吧?)
后两次尝试遵循 older documentation。
我应该如何存根 File.exist? SCHEMA_FILENAME
以便它 returns false
即使文件确实存在?
N.B。这些类似问题中的方法不起作用:
- rspec to stub several File.exists? calls
- Rspec -- need to stub File.open that gets called in another file
- rspec undefined method stub for model
- how to reset expectations on a mocked class method?
- How do I stub out a specific file with rspec?
- Can't stub things with Rspec
- rspec 3 - stub a class method
你在嘲笑 File.exists?
并调用 File.exist?
。
请注意,如果您设置了 verify_partial_doubles
,并且您应该设置,RSpec 将不允许您模拟不存在的方法。
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
RSpec.describe File do
it 'cannot mock a method whcih does not exist' do
allow(File).to receive(:dslkfjalk).and_return(true)
end
end
Failures:
1) File cannot mock a method whcih does not exist
Failure/Error: allow(File).to receive(:dslkfjalk).and_return(true)
File does not implement: dslkfjalk
但是 File
确实有 exist?
和 exists?
两种方法。 File.exists?
is deprecated 并从文档中删除,但我想它仍然...存在。
我想测试以下 ruby 代码的 else
分支:
SCHEMA_SOURCE = if File.exist? SCHEMA_FILENAME
SCHEMA_FILENAME
else
ContentfulApi::Http
end
我找不到模拟文件不存在的方法 SCHEMA_FILENAME
。下面是我试过的简化版测试,报错:
it 'fails to find file when it does not exist' do
allow(File).to receive(:exists?).and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
it 'fails to find file when it does not exist' do
allow(File).to receive(:exists?).with(ContentfulApi::SCHEMA_FILENAME)\
.and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
这些都因错误而失败
expected: falsey value
got: true
it 'fails to find file when it does not exist' do
File.stub(:exists?) { false }
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
it 'fails to find file when it does not exist' do
File.stub!(:exists?).with(ContentfulApi::SCHEMA_FILENAME)\
.and_return(false)
expect(File.exist?(ContentfulApi::SCHEMA_FILENAME)).to be_falsey
end
这些都因错误而失败
undefined method `stub' for File:Class
我的前两次尝试遵循 Method Stubs 文档中的示例,尽管 类 和该文档中未探讨的对象之间可能存在区别。 (虽然 类 是 Ruby 中的对象,对吧?)
后两次尝试遵循 older documentation。
我应该如何存根 File.exist? SCHEMA_FILENAME
以便它 returns false
即使文件确实存在?
N.B。这些类似问题中的方法不起作用:
- rspec to stub several File.exists? calls
- Rspec -- need to stub File.open that gets called in another file
- rspec undefined method stub for model
- how to reset expectations on a mocked class method?
- How do I stub out a specific file with rspec?
- Can't stub things with Rspec
- rspec 3 - stub a class method
你在嘲笑 File.exists?
并调用 File.exist?
。
请注意,如果您设置了 verify_partial_doubles
,并且您应该设置,RSpec 将不允许您模拟不存在的方法。
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
RSpec.describe File do
it 'cannot mock a method whcih does not exist' do
allow(File).to receive(:dslkfjalk).and_return(true)
end
end
Failures:
1) File cannot mock a method whcih does not exist
Failure/Error: allow(File).to receive(:dslkfjalk).and_return(true)
File does not implement: dslkfjalk
但是 File
确实有 exist?
和 exists?
两种方法。 File.exists?
is deprecated 并从文档中删除,但我想它仍然...存在。