I have problems with using stubs, and not sure how should I test this class method:
class Artist < ActiveRecord::Base
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      Artist.find_or_create_by(row.to_hash)
    end
  end
end
RSpec.describe Artist, type: :model do
  describe ".import" do
    before :each do
      create(:artist, name: 'artist1', facebook_url: nil)
      create(:artist, name: 'artist2', facebook_url: "facebook_url")
    end
    context "when facebook_url is empty" do
      it "updates facebook_url" do
        #Here I should somehow stub CSV with name, facebook_url
        #headers and row: artist1,artist1_facebook_url - HOW ?
        Artist.import(file)
        expect(Artist.find_by(name: 'artist1').facebook_url).to eq "artist1_facebook_url"
      end
    end
  end
end
How should I stub it to make this test work ?
You can write something like:
file = CSV.generate do |csv|
  csv << ["name", "facebook_url"] #hash keys
  csv << ["artict1", "nil"]
  csv << ["artict2", "facebook_url"]
end 
# String instead of file
Then test it:
expect(File).to receive(:open).with("filename", "r").and_return(file)  
Artist.import("filename")
expect(Artist.find_by(name: 'artist1').facebook_url).to eq "artist1_facebook_url"
                        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