I'm following this solution to validate a url in a service class in a rails app. The test returns false, but in the console no error is raised when I parse the given url with Ruby's URI class. I don't think there are any typos in my code - any suggestions where I'm going wrong?
class UrlService
attr_accessor :uri
def initialize(uri)
@uri = uri
end
def valid?
begin
uri = URI.parse(uri)
!uri.host.nil? && uri.kind_of?(URI::HTTP)
rescue URI::InvalidURIError
false
end
end
end
test
require "rails_helper"
RSpec.describe UrlService do
subject {described_class.new(url)}
describe "#valid?" do
context "success" do
let(:url) { "https://www.google.com/" }
it "returns true" do
expect(subject.valid?).to be_truthy
end
end
end
end