Using Rails 5.2.3. Getting an error in Rspec. I believe the error is because I am mistyping or misusing the expect method.
Failure/Error: expect(SongsController.stub).to receive(:send_file)
ArgumentError: wrong number of arguments (given 0, expected 1..2)
The code from the controller works in the browser.
send_file "amazing_grace.zip", :filename => "amazing_grace.zip", :url_based_filename => false, :type => "application/zip"
The test code is the following:
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
How would I correct the expect method, to no longer get this error?
This is what worked for me. This passes.
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')
To make the test fail, all tests should fail before passing, I inserted a wrong argument in 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')
This gave a
Failure/Error: send_file "amazing_grace_download.zip", "filename => "amazing_grace_download.zip", :url_based_filename => false, :type => "application/zip".
But it also gave this message
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})
So, I know the test is running, because incorrect arguments means failure, and the test passes with correct arguments.