Skip to content

Instantly share code, notes, and snippets.

@shingonoide
Forked from RafaelPrallon/check_url.md
Last active September 9, 2017 03:44
Show Gist options
  • Save shingonoide/d63ead8d899f39ce0ab8345d58a37935 to your computer and use it in GitHub Desktop.
Save shingonoide/d63ead8d899f39ce0ab8345d58a37935 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://

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)>'
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment