Skip to content

Instantly share code, notes, and snippets.

@RafaelPrallon
Last active September 9, 2017 13:37
Show Gist options
  • Save RafaelPrallon/4e6c26b31e22d5560382d40cd57b7dfe to your computer and use it in GitHub Desktop.
Save RafaelPrallon/4e6c26b31e22d5560382d40cd57b7dfe to your computer and use it in GitHub Desktop.
método, spec e erros no terminal de um método para verificar se a url tem http:// ou https:// e a solução para este problema

método(localizado em app/helpers/application_helper.rb):

def check_url(url)
  return true if /^(?:(?:https\:\/\/))|(?:(?:http\:\/\/))/.match(url)
  return false
end

spec(localizado em spec/models/user_spec.rb):

describe "check_url" do
  include ActionView::Helpers
	it "should return false if url does not have http or https" do
		@user = FactoryGirl.create(:user)
		@user.website = "test-test.test.test"
		expect(check_url(@user.website)).to eql(false)
	end
	it "should return true if url does have http" do
		@user = FactoryGirl.create(:user)
		@user.website = "http://test-test.test.test"
		expect(check_url(@user.website)).to eql(true)
	end
	it "should return true if url does have https" do
		@user = FactoryGirl.create(:user)
		@user.website = "https://test-test.test.test"
		expect(check_url(@user.website)).to eql(true)
	end
end

erros(vindos do terminal):

1) User check_url should return false if url does not have http or https
     Failure/Error: expect(check_url(@user.website)).to eql(false)
     
     NoMethodError:
       undefined method `check_url' for #<RSpec::ExampleGroups::User::CheckUrl:0x000027ddbee628>
       Did you mean?  check_box
     # ./spec/models/user_spec.rb:56:in `block (3 levels) in <top (required)>'

  2) User check_url should return true if url does have http
     Failure/Error: expect(check_url(@user.website)).to eql(true)
     
     NoMethodError:
       undefined method `check_url' for #<RSpec::ExampleGroups::User::CheckUrl:0x000027dec9fbe8>
       Did you mean?  check_box
     # ./spec/models/user_spec.rb:61:in `block (3 levels) in <top (required)>'

  3) User check_url should return true if url does have https
     Failure/Error: expect(check_url(@user.website)).to eql(true)
     
     NoMethodError:
       undefined method `check_url' for #<RSpec::ExampleGroups::User::CheckUrl:0x000027ded48608>
       Did you mean?  check_box
     # ./spec/models/user_spec.rb:66:in `block (3 levels) in <top (required)>'

Solução

Criar uma pasta chamada helpers dentro da pasta de specs e, nessa pasta, criar um arquivo chamado application_helper_spec.rb.

O conteúdo desse spec deve ser:

(em /spec/helpers/application_helper_spec.rb)

require "rails_helper"

RSpec.describe ApplicationHelper, :type => :helper do
  describe "#check_url" do
    it "should return false if url does not have http or https" do
			website = "test-test.test.test"
			expect(check_url(website)).to eql(false)
		end
		it "should return true if url does have http" do
			website = "http://test-test.test.test"
			expect(check_url(website)).to eql(true)
		end
		it "should return true if url does have https" do
			website = "https://test-test.test.test"
			expect(check_url(website)).to eql(true)
		end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment