I have a method that uses CSV.foreach method. The data should look like that:
1,0,0,1
1,1,0,0
I want to write a test which expects an error if I the number of elements is not the same in each line.
Something like:
it "should raise an error if input file lines size differ" do
expect { my_method(invalid_file_name) }.to raise_error
end
How do I mock CSV's foreach method to return invalid input?
You can indeed stub CSV's foreach
method, but you need to use the and_yield
method and chaining to generate more than one line, to wit:
CSV.stub(:foreach).and_yield(<first_line>).and_yield(<second_line)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With