Skip to content

Commit

Permalink
Adds a passing test for HttpValidator Module
Browse files Browse the repository at this point in the history
  • Loading branch information
5minpause committed May 19, 2015
1 parent 3585ed1 commit fa22771
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 17 deletions.
33 changes: 16 additions & 17 deletions Gemfile.lock
Expand Up @@ -38,7 +38,7 @@ GIT
PATH
remote: .
specs:
goldencobra (1.4.31)
goldencobra (1.4.32)
activeadmin
activeadmin-cancan
acts-as-taggable-on
Expand Down Expand Up @@ -158,14 +158,12 @@ GEM
terminal-table (~> 1.4)
builder (3.0.4)
cancan (1.6.10)
capistrano (2.15.5)
highline
net-scp (>= 1.0.0)
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0)
capistrano-maintenance (0.0.4)
capistrano (>= 2.0.0)
capistrano (3.4.0)
i18n
rake (>= 10.0.0)
sshkit (~> 1.3)
capistrano-maintenance (1.0.0)
capistrano (>= 3.0)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand Down Expand Up @@ -193,6 +191,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.8.0)
colorize (0.7.7)
columnize (0.9.0)
compass (0.12.7)
chunky_png (~> 1.2)
Expand Down Expand Up @@ -315,7 +314,7 @@ GEM
launchy (2.4.3)
addressable (~> 2.3)
libv8 (3.16.14.7)
liquid (3.0.1)
liquid (3.0.2)
listen (2.10.0)
celluloid (~> 0.16.0)
rb-fsevent (>= 0.9.3)
Expand All @@ -339,11 +338,7 @@ GEM
nenv (0.2.0)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
net-sftp (2.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.9.2)
net-ssh-gateway (1.2.0)
net-ssh (>= 2.6.5)
newrelic_rpm (3.12.0.288)
nokogiri (1.5.11)
notiffany (0.0.6)
Expand Down Expand Up @@ -444,7 +439,7 @@ GEM
execjs
rails (>= 3.1)
react-source (~> 0.13)
react-source (0.13.2)
react-source (0.13.3)
redcarpet (3.2.3)
redis (3.2.1)
redis-namespace (1.5.2)
Expand Down Expand Up @@ -483,8 +478,8 @@ GEM
ruby_parser (3.6.6)
sexp_processor (~> 4.1)
rubyzip (1.1.7)
rvm-capistrano (1.5.6)
capistrano (~> 2.15.4)
rvm-capistrano (1.5.0)
capistrano (>= 2.15.4)
sass (3.2.19)
sass-rails (3.2.6)
railties (~> 3.2.0)
Expand Down Expand Up @@ -520,6 +515,10 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sshkit (1.7.1)
colorize (>= 0.7.0)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
sunspot (2.0.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)
Expand Down
73 changes: 73 additions & 0 deletions test/dummy/spec/models/http_validator_spec.rb
@@ -0,0 +1,73 @@
# encoding: utf-8

require 'spec_helper'

class DummyClass
# http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
# http://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-define_attribute_method
include ActiveModel::AttributeMethods
define_attribute_method :url_attr
attr_accessor :url_attr

def attributes
{ 'url_attr' => @url_attr }
end

# http://jeffgardner.org/2011/05/26/adding-activerecord-style-callbacks-to-activeresource-models/
extend ActiveModel::Callbacks
define_model_callbacks :save, only: [:before]
def save
run_callbacks :save do
# Your save action methods here
end
end

extend HttpValidator
web_url :url_attr
end

describe HttpValidator do
context "without protocol" do
subject { DummyClass.new }

it "adds 'http://' if missing" do
subject.url_attr = "www.google.de"

subject.save

expect(subject.url_attr).to eq("http://www.google.de")
end

it "adds nothing if nothing is present" do
subject.url_attr = ""

subject.save

expect(subject.url_attr).to eq("")
end
end

context "with http" do
subject { DummyClass.new }

it "adds nothing if 'http://' is present" do
subject.url_attr = "http://www.google.de"

subject.save

expect(subject.url_attr).to eq("http://www.google.de")
end
end

context "with https" do
subject { DummyClass.new }

it "adds nothing if 'https://' is present" do
subject.url_attr = "https://www.google.de"

subject.save

expect(subject.url_attr).to eq("https://www.google.de")
end
end
end

0 comments on commit fa22771

Please sign in to comment.