gpt4 book ai didi

ruby - Rspec——需要 stub File.open 在另一个文件中被调用

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:39 26 4
gpt4 key购买 nike

在我的测试中,我使用一些参数初始化了一个名为 Package 的新类。

在这个类的初始化中,我打开了一个文件,该文件在我的远程机器上可用,但在本地通常不存在。我想知道如何在我的测试中 stub 该方法。

我正在使用 rspec 和 mocha。我试过类似的东西:

File.stubs(:open).with(:file).returns(File.open("#{package_root}/test_files/test.yml"))

在我的测试中初始化 Package 之前,我有这一行。

我遇到了这个错误:

unexpected invocation: File.open('package/test_files/test.yml')
satisfied expectations:
- allowed any number of times, not yet invoked: File.open(:file)

我不太熟悉 rspec 或 mocha,因此不胜感激。谢谢!

最佳答案

stub 的新语法如下所示:

allow(File).to receive(:open).with('file_name').and_return(file_like_object)

关于ruby - Rspec——需要 stub File.open 在另一个文件中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com