From 512987cc7abcb7135746c89b3638e8a35bece042 Mon Sep 17 00:00:00 2001 From: Antonio C Nalesso Moreira Date: Sat, 6 Jul 2013 20:20:35 +0100 Subject: [PATCH] clean up tests, added dynamic support to rb 1.8.7 to 2.x when running tests --- impressionist.gemspec | 8 +- lib/impressionist/counter_cache.rb | 4 +- .../impressionist/impressionable.rb | 10 +- .../impressionist/impressionable.rb | 15 +- .../mongoid/impressionist/impressionable.rb | 2 + test_app/Gemfile | 2 +- .../controllers/articles_controller_spec.rb | 60 ++++++++ test_app/spec/controllers/controller_spec.rb | 134 ------------------ .../spec/controllers/dummy_controller_spec.rb | 11 ++ .../spec/controllers/posts_controller_spec.rb | 19 +++ .../controllers/widgets_controller_spec.rb | 60 ++++++++ .../spec/initializers/initializers_spec.rb | 8 +- test_app/spec/spec_helper.rb | 2 + 13 files changed, 184 insertions(+), 151 deletions(-) create mode 100644 test_app/spec/controllers/articles_controller_spec.rb delete mode 100644 test_app/spec/controllers/controller_spec.rb create mode 100644 test_app/spec/controllers/dummy_controller_spec.rb create mode 100644 test_app/spec/controllers/posts_controller_spec.rb create mode 100644 test_app/spec/controllers/widgets_controller_spec.rb diff --git a/impressionist.gemspec b/impressionist.gemspec index 603fa8a..2b45fc2 100644 --- a/impressionist.gemspec +++ b/impressionist.gemspec @@ -19,8 +19,12 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version= s.add_dependency 'httpclient', '~> 2.2' - s.add_dependency 'nokogiri', '~> 1.5' - s.add_development_dependency 'capybara' + + # Nokogiri has dropped support for Ruby 1.8.7 onwards version 1.5.10 + s.add_dependency 'nokogiri', (RUBY_VERSION.match("1.8.7") ? '1.5.10' : '~> 1.6.0') + + # Capybara has dropped support for Ruby 1.8.7 onwards version 2.0.3 + s.add_development_dependency 'capybara', '>= 2.0.3' s.add_development_dependency 'rake', '>= 0.9' s.add_development_dependency 'rails', '~> 3.1' s.add_development_dependency 'rdoc', '>= 2.4.2' diff --git a/lib/impressionist/counter_cache.rb b/lib/impressionist/counter_cache.rb index 4a7e78f..d1d2898 100644 --- a/lib/impressionist/counter_cache.rb +++ b/lib/impressionist/counter_cache.rb @@ -42,8 +42,10 @@ module Impressionist end # Logs to log file, expects a message to be passed + # default mode is ERROR - def impressionist_log(mode=:error, str) + # ruby 1.8.7 support + def impressionist_log(str, mode=:error) Rails.logger.send(mode.to_s, str) end diff --git a/lib/impressionist/models/active_record/impressionist/impressionable.rb b/lib/impressionist/models/active_record/impressionist/impressionable.rb index d2226ad..053281c 100644 --- a/lib/impressionist/models/active_record/impressionist/impressionable.rb +++ b/lib/impressionist/models/active_record/impressionist/impressionable.rb @@ -2,25 +2,23 @@ ActiveRecord::Base.send(:include, Impressionist::Impressionable) module Impressionist module Impressionable - extend ActiveSupport::Concern module ClassMethods + def is_impressionable(options={}) define_association - imp_cache_options_set(options) + + @impressionist_cache_options = options end +private def define_association has_many(:impressions, :as => :impressionable, :dependent => :destroy) end - def imp_cache_options_set(options) - @impressionist_cache_options = options - end - end end diff --git a/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb b/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb index 2b872e4..1b2ca58 100644 --- a/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb @@ -5,10 +5,17 @@ module Impressionist extend ActiveSupport::Concern module ClassMethods - def is_impressionable(options={}) - many :impressions, :as => :impressionable, :dependent => :destroy - @cache_options = options[:counter_cache] - end + + def is_impressionable(options={}) + many(:impressions, + :as => :impressionable, + :dependent => :destroy) + + @impressionist_cache_options = options + end + end + end + end diff --git a/lib/impressionist/models/mongoid/impressionist/impressionable.rb b/lib/impressionist/models/mongoid/impressionist/impressionable.rb index f8a2b37..d97f986 100644 --- a/lib/impressionist/models/mongoid/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongoid/impressionist/impressionable.rb @@ -1,3 +1,5 @@ +# TODO: Refactor this Entity +# There's a lot of duplication Mongoid::Document.send(:include, Impressionist::Impressionable) module Impressionist diff --git a/test_app/Gemfile b/test_app/Gemfile index 65a819a..48a2b28 100644 --- a/test_app/Gemfile +++ b/test_app/Gemfile @@ -36,9 +36,9 @@ group :development, :test do end group :test do - gem 'capybara' gem 'simplecov' gem 'systemu' + gem 'capybara', '~> 2.0.0' end gem 'jquery-rails' diff --git a/test_app/spec/controllers/articles_controller_spec.rb b/test_app/spec/controllers/articles_controller_spec.rb new file mode 100644 index 0000000..c32ea15 --- /dev/null +++ b/test_app/spec/controllers/articles_controller_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe ArticlesController do + fixtures :articles,:impressions,:posts,:widgets + + render_views + + it "should make the impressionable_hash available" do + get "index" + response.body.include?("false").should eq true + end + + it "should log an impression with a message" do + get "index" + Impression.all.size.should eq 12 + Article.first.impressions.last.message.should eq "this is a test article impression" + Article.first.impressions.last.controller_name.should eq "articles" + Article.first.impressions.last.action_name.should eq "index" + end + + it "should log an impression without a message" do + get "show", :id=> 1 + Impression.all.size.should eq 12 + Article.first.impressions.last.message.should eq nil + Article.first.impressions.last.controller_name.should eq "articles" + Article.first.impressions.last.action_name.should eq "show" + end + + it "should log the user_id if user is authenticated (@current_user before_filter method)" do + session[:user_id] = 123 + get "show", :id=> 1 + Article.first.impressions.last.user_id.should eq 123 + end + + it "should not log the user_id if user is authenticated" do + get "show", :id=> 1 + Article.first.impressions.last.user_id.should eq nil + end + + it "should log the request_hash, ip_address, referrer and session_hash" do + get "show", :id=> 1 + Impression.last.request_hash.size.should eq 64 + Impression.last.ip_address.should eq "0.0.0.0" + Impression.last.session_hash.size.should eq 32 + Impression.last.referrer.should eq nil + end + + # Capybara has change the way it works + # We need to pass :type options in order to make include helper methods + # see https://github.com/jnicklas/capybara#using-capybara-with-rspec + it "should log the referrer when you click a link", :type => :feature do + visit article_url(Article.first) + click_link "Same Page" + Impression.last.referrer.should eq "http://test.host/articles/1" + end +end + + + + diff --git a/test_app/spec/controllers/controller_spec.rb b/test_app/spec/controllers/controller_spec.rb deleted file mode 100644 index aa06c29..0000000 --- a/test_app/spec/controllers/controller_spec.rb +++ /dev/null @@ -1,134 +0,0 @@ -require 'spec_helper' - -describe ArticlesController do - fixtures :articles,:impressions,:posts,:widgets - render_views - - it "should make the impressionable_hash available" do - get "index" - response.body.include?("false").should eq true - end - - it "should log an impression with a message" do - get "index" - Impression.all.size.should eq 12 - Article.first.impressions.last.message.should eq "this is a test article impression" - Article.first.impressions.last.controller_name.should eq "articles" - Article.first.impressions.last.action_name.should eq "index" - end - - it "should log an impression without a message" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - Article.first.impressions.last.message.should eq nil - Article.first.impressions.last.controller_name.should eq "articles" - Article.first.impressions.last.action_name.should eq "show" - end - - it "should log the user_id if user is authenticated (@current_user before_filter method)" do - session[:user_id] = 123 - get "show", :id=> 1 - Article.first.impressions.last.user_id.should eq 123 - end - - it "should not log the user_id if user is authenticated" do - get "show", :id=> 1 - Article.first.impressions.last.user_id.should eq nil - end - - it "should log the request_hash, ip_address, referrer and session_hash" do - get "show", :id=> 1 - Impression.last.request_hash.size.should eq 64 - Impression.last.ip_address.should eq "0.0.0.0" - Impression.last.session_hash.size.should eq 32 - Impression.last.referrer.should eq nil - end - - it "should log the referrer when you click a link" do - visit article_url(Article.first) - click_link "Same Page" - Impression.last.referrer.should eq "http://test.host/articles/1" - end -end - -describe PostsController do - it "should log impression at the action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - Impression.last.controller_name.should eq "posts" - Impression.last.action_name.should eq "show" - Impression.last.impressionable_type.should eq "Post" - Impression.last.impressionable_id.should eq 1 - end - - it "should log the user_id if user is authenticated (current_user helper method)" do - session[:user_id] = 123 - get "show", :id=> 1 - Post.first.impressions.last.user_id.should eq 123 - end -end - -describe WidgetsController do - - before(:each) do - @widget = Widget.find(1) - Widget.stub(:find).and_return(@widget) - end - - it "should log impression at the per action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "index" - Impression.all.size.should eq 13 - get "new" - Impression.all.size.should eq 13 - end - - it "should not log impression when user-agent is in wildcard list" do - request.stub!(:user_agent).and_return('somebot') - get "show", :id=> 1 - Impression.all.size.should eq 11 - end - - it "should not log impression when user-agent is in the bot list" do - request.stub!(:user_agent).and_return('Acoon Robot v1.50.001') - get "show", :id=> 1 - Impression.all.size.should eq 11 - end - - describe "impressionist unique options" do - - it "should log unique impressions at the per action level" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "show", :id=> 2 - Impression.all.size.should eq 13 - get "show", :id => 2 - Impression.all.size.should eq 13 - get "index" - Impression.all.size.should eq 14 - end - - it "should log unique impressions only once per id" do - get "show", :id=> 1 - Impression.all.size.should eq 12 - get "show", :id=> 2 - Impression.all.size.should eq 13 - get "show", :id => 2 - Impression.all.size.should eq 13 - get "index" - Impression.all.size.should eq 14 - end - - end - -end -describe DummyController do - fixtures :impressions - render_views - - it "should log impression at the per action level on non-restful controller" do - get "index" - Impression.all.size.should eq 12 - end -end diff --git a/test_app/spec/controllers/dummy_controller_spec.rb b/test_app/spec/controllers/dummy_controller_spec.rb new file mode 100644 index 0000000..635b0f6 --- /dev/null +++ b/test_app/spec/controllers/dummy_controller_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe DummyController do + fixtures :impressions + render_views + + it "should log impression at the per action level on non-restful controller" do + get "index" + Impression.all.size.should eq 12 + end +end diff --git a/test_app/spec/controllers/posts_controller_spec.rb b/test_app/spec/controllers/posts_controller_spec.rb new file mode 100644 index 0000000..78cbe84 --- /dev/null +++ b/test_app/spec/controllers/posts_controller_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe PostsController do + it "should log impression at the action level" do + get "show", :id=> 1 + Impression.all.size.should eq 12 + Impression.last.controller_name.should eq "posts" + Impression.last.action_name.should eq "show" + Impression.last.impressionable_type.should eq "Post" + Impression.last.impressionable_id.should eq 1 + end + + it "should log the user_id if user is authenticated (current_user helper method)" do + session[:user_id] = 123 + get "show", :id=> 1 + Post.first.impressions.last.user_id.should eq 123 + end + +end diff --git a/test_app/spec/controllers/widgets_controller_spec.rb b/test_app/spec/controllers/widgets_controller_spec.rb new file mode 100644 index 0000000..313c250 --- /dev/null +++ b/test_app/spec/controllers/widgets_controller_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe WidgetsController do + + before(:each) do + @widget = Widget.find(1) + Widget.stub(:find).and_return(@widget) + end + + it "should log impression at the per action level" do + get "show", :id=> 1 + Impression.all.size.should eq 12 + get "index" + Impression.all.size.should eq 13 + get "new" + Impression.all.size.should eq 13 + end + + it "should not log impression when user-agent is in wildcard list" do + request.stub!(:user_agent).and_return('somebot') + get "show", :id=> 1 + Impression.all.size.should eq 11 + end + + it "should not log impression when user-agent is in the bot list" do + request.stub!(:user_agent).and_return('Acoon Robot v1.50.001') + get "show", :id=> 1 + Impression.all.size.should eq 11 + end + + context "impressionist unique options" do + + it "should log unique impressions at the per action level" do + get "show", :id=> 1 + Impression.all.size.should eq 12 + get "show", :id=> 2 + Impression.all.size.should eq 13 + get "show", :id => 2 + Impression.all.size.should eq 13 + get "index" + Impression.all.size.should eq 14 + end + + it "should log unique impressions only once per id" do + get "show", :id=> 1 + Impression.all.size.should eq 12 + + get "show", :id=> 2 + Impression.all.size.should eq 13 + + get "show", :id => 2 + Impression.all.size.should eq 13 + + get "index" + Impression.all.size.should eq 14 + end + + end + +end diff --git a/test_app/spec/initializers/initializers_spec.rb b/test_app/spec/initializers/initializers_spec.rb index 879a82a..398f49b 100644 --- a/test_app/spec/initializers/initializers_spec.rb +++ b/test_app/spec/initializers/initializers_spec.rb @@ -1,14 +1,16 @@ require 'spec_helper' describe Impressionist do + let(:imp) { RUBY_VERSION.match("1.8") ? "is_impressionable" : :is_impressionable } + it "should be extended from ActiveRecord::Base" do - method = RUBY_VERSION.match("1.8") ? "is_impressionable" : :is_impressionable - ActiveRecord::Base.methods.include?(method).should be_true + expect(ActiveRecord::Base).to respond_to(imp) + #ActiveRecord::Base.methods.include?(method).should be_true end it "should include methods in ApplicationController" do method = RUBY_VERSION.match("1.8") ? "impressionist" : :impressionist - ApplicationController.instance_methods.include?(method).should be_true + expect(ApplicationController).to respond_to(method) end it "should include the before_filter method in ApplicationController" do diff --git a/test_app/spec/spec_helper.rb b/test_app/spec/spec_helper.rb index 9cbe63b..a01e29b 100644 --- a/test_app/spec/spec_helper.rb +++ b/test_app/spec/spec_helper.rb @@ -5,6 +5,8 @@ unless ENV['CI'] end require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' +require 'capybara/rails' +require 'capybara/rspec' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories.