From 57141fc3059a76b3366820dd9abd0c118bfbbc37 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Wed, 12 Jan 2011 21:57:05 -0800 Subject: [PATCH] Pull logging out to a shared behavior. Remove 4 pending specs. --- spec/controllers/aspects_controller_spec.rb | 17 ++++ spec/controllers/home_controller_spec.rb | 106 +++----------------- spec/shared_behaviors/log_override.rb | 65 ++++++++++++ 3 files changed, 95 insertions(+), 93 deletions(-) create mode 100644 spec/shared_behaviors/log_override.rb diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 73cd0680a..06db2b8e1 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -3,6 +3,7 @@ # the COPYRIGHT file. require 'spec_helper' +require File.join(Rails.root, "spec", "shared_behaviors", "log_override") describe AspectsController do render_views @@ -25,6 +26,22 @@ describe AspectsController do request.env["HTTP_REFERER"] = 'http://' + request.host end + describe "custom logging on success" do + before do + @action = :index + @expected_renders = 15 + end + it_should_behave_like "it overrides the logs on success" + end + + describe "custom logging on redirect" do + before do + @action = :show + @action_params = {'id' => @aspect0.id.to_s} + end + it_should_behave_like "it overrides the logs on redirect" + end + describe "#index" do it "assigns @contacts to all the user's contacts" do Factory.create :person diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 802d6f7c1..0c18ef5fb 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -3,6 +3,7 @@ # the COPYRIGHT file. require 'spec_helper' +require File.join(Rails.root, "spec", "shared_behaviors", "log_override") describe HomeController do render_views @@ -26,102 +27,21 @@ describe HomeController do end end - #This describe should apply to any controller class. HomeController is just the simplest. - describe 'log overriding in lib/log_overrider' do + describe "custom logging on success" do before do - Rails.stub(:logger).and_return(FakeLogger.new) - end - context 'cross-stage' do - before do - pending "This might require patching Rails" - get :show - @lines = Rails.logger.infos - @id = @lines[1].match(/r_id=(\w+)\s/).captures.first - end - it 'logs a unified id in a request' do - id = @lines.first.match(/r_id=(\w+)\s/).captures.first - @lines.each do |line| - line.match(/r_id=(\w+)\s/).captures.first.should == @id - end - end - it 'logs different ids in different requests' do - get :show - old_lines = Rails.logger.infos.select do |line| - line.match(/r_id=(\w+)\s/).captures.first == @id - end - old_lines.length.should == Rails.logger.infos.length/2 - end - end - context 'starting' do - before do - pending "This code is never reached in tests, but it seems to work in actual requests" - get :show - @line = Rails.logger.infos.first - end - it 'logs it' do - @line.should match /event=request_started/ - end - end - context 'rendering' do - before do - get :show, :lasers => 'green' - @lines = Rails.logger.infos.select{|l| l.include?("event=render")} - end - it 'logs all renders' do - @lines.length.should == 1 - end - it 'logs layouts' do - pending 'where is the template=home/show line?' - home_line = @lines.detect{|t| - t.include?("template=home/show.html.haml")} - home_line.should match /layout=layouts\/application/ - end - - end - context 'completion' do - context 'ok' do - before do - get :show, :lasers => 'green' - @line = Rails.logger.infos.last - end - it 'logs the completion of a request' do - @line.include?('event=request_completed').should be_true - end - it 'logs an ok' do - @line.include?('status=200').should be_true - end - it 'logs the controller' do - @line.include?('controller=HomeController').should be_true - end - it 'logs the action' do - @line.include?('action=show').should be_true - end - it 'logs params' do - @line.include?("params='{\"lasers\"=>\"green\"}'").should be_true - end - it 'does not log the view rendering time addition' do - @line.include?("(Views: ").should be_false - end - end - context 'redirected' do - before do - sign_in @user - get :show, :lasers => 'green' - @line = Rails.logger.infos.last - end - it 'logs a redirect' do - @line.include?('status=302').should be_true - end - end + @action = :show + @action_params = {"lasers" => "green"} + @expected_renders = 1 end + it_should_behave_like "it overrides the logs on success" end - class FakeLogger - attr_accessor :infos - def initialize - self.infos = [] - end - def info line - self.infos << line + + describe "custom logging on redirect" do + before do + sign_in :user, make_user + @action = :show + @action_params = {"lasers" => "green"} end + it_should_behave_like "it overrides the logs on redirect" end end diff --git a/spec/shared_behaviors/log_override.rb b/spec/shared_behaviors/log_override.rb new file mode 100644 index 000000000..a51a3c28e --- /dev/null +++ b/spec/shared_behaviors/log_override.rb @@ -0,0 +1,65 @@ +class FakeLogger + attr_accessor :infos + + def initialize + self.infos = [] + end + + def info line + self.infos << line + end +end + +shared_examples_for 'it overrides the logs on success' do + before do + Rails.stub(:logger).and_return(FakeLogger.new) + end + context 'rendering' do + before do + get @action, @action_params + @lines = Rails.logger.infos.select { |l| l.include?("event=render") } + end + it 'logs all renders' do + @lines.length.should == @expected_renders + end + end + context 'completion' do + context 'ok' do + before do + get @action, @action_params + @line = Rails.logger.infos.last + end + it 'logs the completion of a request' do + @line.include?('event=request_completed').should be_true + end + it 'logs an ok' do + @line.include?('status=200').should be_true + end + it 'logs the controller' do + @line.include?("controller=#{controller.class.name}").should be_true + end + it 'logs the action' do + @line.include?("action=#{@action}").should be_true + end + it 'logs params' do + if @action_params + @line.include?("params='#{@action_params.inspect.gsub(" ", "")}'").should be_true + end + end + it 'does not log the view rendering time addition' do + @line.include?("(Views: ").should be_false + end + end + end +end + +shared_examples_for 'it overrides the logs on redirect' do + before do + Rails.stub(:logger).and_return(FakeLogger.new) + get @action, @action_params + @line = Rails.logger.infos.last + end + it 'logs a redirect' do + @line.include?('status=302').should be_true + end +end