Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么使用File.open调用一次会导致根据rspec调用它3次_Ruby On Rails_Ruby_File_Rspec - Fatal编程技术网

Ruby on rails 为什么使用File.open调用一次会导致根据rspec调用它3次

Ruby on rails 为什么使用File.open调用一次会导致根据rspec调用它3次,ruby-on-rails,ruby,file,rspec,Ruby On Rails,Ruby,File,Rspec,下面您可以看到我正在调用File.open只有一次,但是rspec告诉我它收到了三次 def self.import_file(filename) current_file = filename.split('/').last destroy_all(["filename = ?",current_file]) unpack_format = "A#{INPUT_FILE_FORMAT.map{|element| element[1]}.join("A")}" debugger

下面您可以看到我正在调用
File.open
只有一次,但是rspec告诉我它收到了三次

def self.import_file(filename)
  current_file = filename.split('/').last
  destroy_all(["filename = ?",current_file])

  unpack_format = "A#{INPUT_FILE_FORMAT.map{|element| element[1]}.join("A")}"
  debugger
  File.open(filename, 'r').each do |line|
    hash_created = create_hash(line, unpack_format).merge({:filename=>current_file})
    create(hash_created)
  end
end  

it "should delete previous records with the same filename" do
  Payrec.should_receive(:destroy_all).with(["filename = ?", "testfile.txt"])
  File.should_receive(:open).and_return([@file_line])
  Payrec.import_file "testfile.txt"
end
输出是

<File (class)> expected :open with (any args) once, but received it 3 times
应为:使用(任何参数)打开一次,但收到三次

每个人和他的狗都在调用
文件。打开
。我可以想象它调用的原因有很多:RSpec读取其配置文件,Rails读取其配置文件,Cucumber读取其配置文件,调试器创建临时文件,其他东西创建临时文件等等

您应该检查是谁在调用
File.open
,调用发生在哪里,参数是什么,以及发生的原因

但是,在设定核心方法的期望值时,这是您必须处理的事情

例如,想象一下,您正在Rubinius上运行您的规范。在Rubinius中,编译器是用Ruby编写的。它目前并不缓存编译后的结果,但可以肯定的是,它可以缓存这些结果,然后自然地使用
File.open
。砰!现在,您的规格随机中断取决于您是否达到JIT阈值


或者,更糟糕的是:所有Rubinius都广泛使用数组和符号来实现几乎所有东西。试着对这些设定一些期望

每个人和他的狗都在叫
文件。打开
。我可以想象它调用的原因有很多:RSpec读取其配置文件,Rails读取其配置文件,Cucumber读取其配置文件,调试器创建临时文件,其他东西创建临时文件等等

您应该检查是谁在调用
File.open
,调用发生在哪里,参数是什么,以及发生的原因

但是,在设定核心方法的期望值时,这是您必须处理的事情

例如,想象一下,您正在Rubinius上运行您的规范。在Rubinius中,编译器是用Ruby编写的。它目前并不缓存编译后的结果,但可以肯定的是,它可以缓存这些结果,然后自然地使用
File.open
。砰!现在,您的规格随机中断取决于您是否达到JIT阈值


或者,更糟糕的是:所有Rubinius都广泛使用数组和符号来实现几乎所有东西。试着对这些设定一些期望

这还远远不够完美,但为了快速了解这些File.open调用实际上在做什么,请在主脚本的顶部添加以下内容:

class File
  class << self
   alias_method :old_open, :open
    def open(*args, &block)
      p args
      block ? block.call(old_open(*args)) : old_open(*args)
    end
  end
end
类文件

class这远不是完美的,但是作为一个快速的黑客来找出这些File.open调用实际上在做什么,请在主脚本的顶部添加以下内容:

class File
  class << self
   alias_method :old_open, :open
    def open(*args, &block)
      p args
      block ? block.call(old_open(*args)) : old_open(*args)
    end
  end
end
类文件

类添加第二个调用以导入_文件是否会将调用数移动到4或6?如果是4,不管您执行了什么操作,都可能有三个调用,您可能需要深入挖掘才能找到它们。如果是6,则调用次数是您认为的3倍。添加第二个调用以导入_文件是否会将调用次数移动到4或6?如果是4,不管您执行了什么操作,都可能有三个调用,您可能需要深入挖掘才能找到它们。如果是6,则调用它的次数是您认为的3倍。此测试问题的一个解决方案是提取
文件。在您自己的代码中打开
调用到一个单独的方法中,并简单地断言该方法已被调用。此测试问题的一个解决方案是提取
文件。打开
调用到一个单独的方法中在您自己的代码中,只需声明调用了该方法。