Rspec expect method gives ArgumentError: wrong number of arguments (given 0, expected 1..2)

Rspec expect method gives ArgumentError: wrong number of arguments (given 0, expected 1..2)

使用Rails 5.2.3。在 Rspec 中出现错误。我认为错误是因为我输入错误或误用了 expect 方法。

Failure/Error: expect(SongsController.stub).to receive(:send_file)
ArgumentError: wrong number of arguments (given 0, expected 1..2)

来自控制器的代码在浏览器中有效。

send_file "amazing_grace.zip", :filename => "amazing_grace.zip", :url_based_filename => false, :type => "application/zip"

测试代码如下:

require 'rails_helper'
RSpec.describe 'Songs features' do
  describe 'viewing the index' do
    it 'downloads zip file from amazon' do
      expect(SongsController.stub).to receive(:send_file)
      visit('/songs/get_zip/1')
    end
  end
end

如何更正 expect 方法,不再出现此错误?

这对我有用。这样就过去了。

expect_any_instance_of(SongsController).to receive(:send_file).with("amazing_grace_download.zip", "filename => "amazing_grace_download.zip", :url_based_filename => false, :type => "application/zip").and_call_original
visit('/songs/get_zip/1')

为了使测试失败,所有测试都应该在通过之前失败,我在 with() 中插入了一个错误的参数。

expect_any_instance_of(SongsController).to receive(:send_file).with("xxx.zip", "filename => "amazing_grace_download.zip", :url_based_filename => false, :type => "application/zip").and_call_original).and_call_original
visit('/songs/get_zip/1')

这给了一个

Failure/Error: send_file "amazing_grace_download.zip", "filename => "amazing_grace_download.zip", :url_based_filename => false, :type => "application/zip".

但它也给出了这个信息

expected: ("xxx.zip", {:filename=>"amazing_grace_downloaded.zip", :type=>"application/zip", :url_based_filename=>false})

got: ("amazing_grace_downloaded.zip", {:filename=>"amazing_grace_downloaded.zip", :type=>"application/zip", :url_based_filename=>false})

所以,我知道测试是运行,因为不正确的参数意味着失败,而测试通过了正确的参数。