From cf1668265c287deba85b41c7938d7a21b466eb2d Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Mon, 6 May 2013 17:32:17 +0200 Subject: [PATCH] Extract test from spec/model/app to spec/model/error_report --- app/models/app.rb | 5 ++++- spec/models/app_spec.rb | 82 ---------------------------------------------------------------------------------- spec/models/error_report_spec.rb | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 84 deletions(-) diff --git a/app/models/app.rb b/app/models/app.rb index a1ade52..f4846d3 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -81,7 +81,10 @@ class App end def find_or_create_err!(attrs) - Err.where(:fingerprint => attrs[:fingerprint]).first || problems.create!.errs.create!(attrs) + Err.where( + :fingerprint => attrs[:fingerprint] + ).first || + problems.create!.errs.create!(attrs) end # Mongoid Bug: find(id) on association proxies returns an Enumerator diff --git a/spec/models/app_spec.rb b/spec/models/app_spec.rb index 69fd691..3b7cfaf 100644 --- a/spec/models/app_spec.rb +++ b/spec/models/app_spec.rb @@ -195,76 +195,6 @@ describe App do ErrorReport.any_instance.stub(:fingerprint).and_return('fingerprintdigest') end - it 'finds the correct app' do - @notice = App.report_error!(@xml) - @notice.err.app.should == @app - end - - it 'finds the correct err for the notice' do - App.should_receive(:find_by_api_key!).and_return(@app) - @app.should_receive(:find_or_create_err!).with({ - :error_class => 'HoptoadTestingException', - :component => 'application', - :action => 'verify', - :environment => 'development', - :fingerprint => 'fingerprintdigest' - }).and_return(err = Fabricate(:err)) - err.notices.stub(:create!) - @notice = App.report_error!(@xml) - end - - it 'marks the err as unresolved if it was previously resolved' do - App.should_receive(:find_by_api_key!).and_return(@app) - @app.should_receive(:find_or_create_err!).with({ - :error_class => 'HoptoadTestingException', - :component => 'application', - :action => 'verify', - :environment => 'development', - :fingerprint => 'fingerprintdigest' - }).and_return(err = Fabricate(:err, :problem => Fabricate(:problem, :resolved => true))) - err.should be_resolved - @notice = App.report_error!(@xml) - @notice.err.should == err - @notice.err.should_not be_resolved - end - - it 'should create a new notice' do - @notice = App.report_error!(@xml) - @notice.should be_persisted - end - - it 'assigns an err to the notice' do - @notice = App.report_error!(@xml) - @notice.err.should be_a(Err) - end - - it 'captures the err message' do - @notice = App.report_error!(@xml) - @notice.message.should == 'HoptoadTestingException: Testing hoptoad via "rake hoptoad:test". If you can see this, it works.' - end - - it 'captures the backtrace' do - @notice = App.report_error!(@xml) - @notice.backtrace_lines.size.should == 73 - @notice.backtrace_lines.last['file'].should == '[GEM_ROOT]/bin/rake' - end - - it 'captures the server_environment' do - @notice = App.report_error!(@xml) - @notice.server_environment['environment-name'].should == 'development' - end - - it 'captures the request' do - @notice = App.report_error!(@xml) - @notice.request['url'].should == 'http://example.org/verify' - @notice.request['params']['controller'].should == 'application' - end - - it 'captures the notifier' do - @notice = App.report_error!(@xml) - @notice.notifier['name'].should == 'Hoptoad Notifier' - end - it "should handle params without 'request' section" do xml = Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read lambda { App.report_error!(xml) }.should_not raise_error @@ -276,18 +206,6 @@ describe App do @notice.backtrace_lines.length.should == 1 end - it 'captures the current_user' do - @notice = App.report_error!(@xml) - @notice.user_attributes['id'].should == '123' - @notice.user_attributes['name'].should == 'Mr. Bean' - @notice.user_attributes['email'].should == 'mr.bean@example.com' - @notice.user_attributes['username'].should == 'mrbean' - end - - it 'captures the framework' do - @notice = App.report_error!(@xml) - @notice.framework.should == 'Rails: 3.2.11' - end end diff --git a/spec/models/error_report_spec.rb b/spec/models/error_report_spec.rb index 1c8151d..89d2dd7 100644 --- a/spec/models/error_report_spec.rb +++ b/spec/models/error_report_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe ErrorReport do context "with notice without line of backtrace" do let(:xml){ - Rails.root.join('spec','fixtures','hoptoad_test_notice_with_one_line_of_backtrace.xml').read + Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read } let(:error_report) { @@ -17,6 +17,12 @@ describe ErrorReport do ) } + describe "#app" do + it 'find the good app' do + expect(error_report.app).to eq app + end + end + describe "#backtrace" do it 'should have valid backtrace' do @@ -32,6 +38,41 @@ describe ErrorReport do app.reload.problems.count }.by(1) end + describe "notice create" do + before { error_report.generate_notice! } + subject { error_report.notice } + its(:message) { 'HoptoadTestingException: Testing hoptoad via "rake hoptoad:test". If you can see this, it works.' } + its(:framework) { should == 'Rails: 3.2.11' } + + it 'has complete backtrace' do + subject.backtrace_lines.size.should == 73 + subject.backtrace_lines.last['file'].should == '[GEM_ROOT]/bin/rake' + end + it 'has server_environement' do + subject.server_environment['environment-name'].should == 'development' + end + + it 'has request' do + subject.request['url'].should == 'http://example.org/verify' + subject.request['params']['controller'].should == 'application' + end + + it 'has notifier' do + subject.notifier['name'].should == 'Hoptoad Notifier' + end + + it 'get user_attributes' do + subject.user_attributes['id'].should == '123' + subject.user_attributes['name'].should == 'Mr. Bean' + subject.user_attributes['email'].should == 'mr.bean@example.com' + subject.user_attributes['username'].should == 'mrbean' + end + end + + it 'save a notice assignes to err' do + error_report.generate_notice! + error_report.notice.err.should be_a(Err) + end it 'memoize the notice' do expect { @@ -41,6 +82,46 @@ describe ErrorReport do Notice.count }.by(1) end + + it 'find the correct err for the notice' do + Fabricate( + :err, { + :fingerprint => 'eeb6cc484167899c061e7859008c4b23bae0851c', + :problem => Fabricate(:problem, :resolved => true) + } + ) + expect { + error_report.generate_notice! + }.to change { + error_report.error.resolved? + }.from(true).to(false) + end + + context "with xml without request section" do + let(:xml){ + Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read + } + it "save a notice" do + expect { + error_report.generate_notice! + }.to change { + app.reload.problems.count + }.by(1) + end + end + + context "with xml with only a single line of backtrace" do + let(:xml){ + Rails.root.join('spec','fixtures','hoptoad_test_notice_with_one_line_of_backtrace.xml').read + } + it "save a notice" do + expect { + error_report.generate_notice! + }.to change { + app.reload.problems.count + }.by(1) + end + end end describe "#valid?" do -- libgit2 0.21.2