You can use allow
(stub) instead of expect
(mock):
allow(object).to receive(:my_method_name) { |param1, param2| param1 }
With named parameters:
allow(object).to receive(:my_method_name) { |params| params[:my_named_param] }
Here is a real life example:
Let's assume we have a S3StorageService
that uploads our files to S3 using the upload_file
method. That method returns the S3 direct URL to our uploaded file.
def self.upload_file(file_type:, pathname:, metadata: {}) …
We want to stub that upload for many reasons (offline testing, performance improvements…):
allow(S3StorageService).to receive(:upload_file) { |params| params[:pathname] }
That stub only returns the file path.