我在使用stubs时遇到了问题,并且不确定应该如何测试这个类方法:
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
我应该如何存根它使这个测试工作?
1条答案
按热度按时间qyzbxkaa1#
你可以这样写:
然后测试它: