【问题标题】:Rspec expect method gives ArgumentError: wrong number of arguments (given 0, expected 1..2)Rspec 期望方法给出 ArgumentError: wrong number of arguments (given 0, expected 1..2)
【发布时间】:2019-07-30 17:37:45
【问题描述】:

使用 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

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

【问题讨论】:

    标签: rspec ruby-on-rails-5 rspec-rails


    【解决方案1】:

    这对我有用。这通过了。

    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')
    

    这给了一个

    失败/错误:send_file "amazing_grace_download.zip", "filename => “amazing_grace_download.zip”,:url_based_filename => false,:type => “应用程序/压缩包”。

    但它也给出了这个信息

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      相关资源
      最近更新 更多