clean up tests, added dynamic support to rb 1.8.7 to 2.x when running tests

This commit is contained in:
Antonio C Nalesso Moreira 2013-07-06 20:20:35 +01:00
parent ec5df5da55
commit 512987cc7a
13 changed files with 184 additions and 151 deletions

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,5 @@
# TODO: Refactor this Entity
# There's a lot of duplication
Mongoid::Document.send(:include, Impressionist::Impressionable)
module Impressionist

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.