Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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 使用rspec间谍“已经收到”匹配器_Ruby On Rails_Rspec_Rake_Matcher - Fatal编程技术网

Ruby on rails 使用rspec间谍“已经收到”匹配器

Ruby on rails 使用rspec间谍“已经收到”匹配器,ruby-on-rails,rspec,rake,matcher,Ruby On Rails,Rspec,Rake,Matcher,现在,我正在尝试设置使用matcher对象的能力。应该已经收到(:action)。带有(*params),但似乎无法获得它。为了解决问题,我写了一个非常简单的rake任务: # lib/tasks/check_log.rake namespace :check_log do # other tasks omitted desc "Task description" task :test => :environment do ImportRecording.log.de

现在,我正在尝试设置使用matcher
对象的能力。应该已经收到(:action)。带有(*params)
,但似乎无法获得它。为了解决问题,我写了一个非常简单的rake任务:

# lib/tasks/check_log.rake
namespace :check_log do
  # other tasks omitted

  desc "Task description"
  task :test => :environment do
    ImportRecording.log.debug "Something"
  end
end
下面是我在
spec/lib/tasks/check\u log\u rake\u spec.rb

require_relative '../../support/shared_contexts/rake'
# other requirements omitted
describe "check_log:test", focus: true do
  include_context "rake"

  let(:log_name) { "import.log" }
  let(:checker) { CheckLog.new log_name }
  let(:import_log) { ImportRecording.log }
  let(:message) { "Asterisk import finished!" }

  it "verify a method was called on a log" do
    subject.invoke
    ImportRecording.log.should have_received(:debug).with("Something")
  end
end
require "rake"

shared_context "rake" do
  let(:rake)      { Rake::Application.new }
  let(:task_name) { self.class.top_level_description }
  let(:task_path) { "lib/tasks/#{task_name.split(":").first}" }
  subject         { rake[task_name] }

  def loaded_files_excluding_current_rake_file
    $".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s }
  end

  before do
    Rake.application = rake
    Rake.application.rake_require(task_path,[Rails.root.to_s],loaded_files_excluding_current_rake_file)

    Rake::Task.define_task(:environment)
  end
end
感谢前面的博文,这里是我的共享上下文,在
spec/support/shared_contexts/rake.rb

require_relative '../../support/shared_contexts/rake'
# other requirements omitted
describe "check_log:test", focus: true do
  include_context "rake"

  let(:log_name) { "import.log" }
  let(:checker) { CheckLog.new log_name }
  let(:import_log) { ImportRecording.log }
  let(:message) { "Asterisk import finished!" }

  it "verify a method was called on a log" do
    subject.invoke
    ImportRecording.log.should have_received(:debug).with("Something")
  end
end
require "rake"

shared_context "rake" do
  let(:rake)      { Rake::Application.new }
  let(:task_name) { self.class.top_level_description }
  let(:task_path) { "lib/tasks/#{task_name.split(":").first}" }
  subject         { rake[task_name] }

  def loaded_files_excluding_current_rake_file
    $".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s }
  end

  before do
    Rake.application = rake
    Rake.application.rake_require(task_path,[Rails.root.to_s],loaded_files_excluding_current_rake_file)

    Rake::Task.define_task(:environment)
  end
end
因此,我安装了
rspec-spies
gem,并将以下行添加到我的
spec\u-helper

require `rspec-spies`
但是当我运行测试时,我得到以下错误:

Failure/Error: ImportRecording.log.should have_received(:debug).with("Something")
   expected #<Logger:0x007ffbbe4638b0 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007ffbbe463108 @datetime_format=nil>,
   @formatter=#<Logger::SimpleFormatter:0x007ffbbe462960 @datetime_format="%F %T">,
   @logdev=#<Logger::LogDevice:0x007ffbbe462f28 @shift_size=1048576, @shift_age=0,
   @filename="/Users/brad/code/ruby/rails_projects/rl4/log/import.log",
   @dev=#<File:/Users/brad/code/ruby/rails_projects/rl4/log/import.log>,
   @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007ffbbe462e60 @mon_owner=nil,
   @mon_count=0, @mon_mutex=#<Mutex:0x007ffbbe462af0>>>,
   @mock_proxy=#<RSpec::Mocks::Proxy:0x007ffbbae76a68 @object=#<Logger:0x007ffbbe4638b0 ...>,
   @name=nil, @error_generator=#<RSpec::Mocks::ErrorGenerator:0x007ffbbae76a18 @declared_as="Mock",
   @target=#<Logger:0x007ffbbe4638b0 ...>, @name=nil>,
   @expectation_ordering=#<RSpec::Mocks::OrderGroup:0x007ffbbae769c8 @ordering=[]>,
   @messages_received=[], @options={}, @already_proxied_respond_to=false,
   @null_object=false>> to have received :debug with ["Something"]
Failure/Error:ImportRecording.log.have_应已接收(:debug).with(“某物”)
预期#已收到:调试[“某物”]
但是,我已经验证了这行代码是否正在编写中。我是否使用了错误的方法,或者有人能指出正确的方向吗?

根据我的说法,我误解了测试间谍。它们实际上是一个两步的过程,你必须

  • Stub您希望调用的方法
  • 调用覆盖的方法或进程
  • 使用
    have_received
    matcher来验证stubbed实际调用了什么
  • 对我的测试进行这些更改已经解决了我所有的问题