Search code examples
rubyrspecwildcard

How to mock the method calls with wildcard parameter in Rspec?


This is my production code

def my_method
    list = [.....]
    list.each do |filename|
        content = File.read(filename)
        ...
    end
end

I am writing an unit test in Rspec, so how to mock the File.read using wildcard parameter?

File.should_receive(:read).with(??some regex here).and_return ""

Thanks in advance!


Solution

  • You can use anything or any_args.

    File.should_receive(:read).with(anything).and_return ""