2

When Letter Opener email is triggered, it get printed into test.log.

I need it to print it under console log.

When Feature spec is running and if exception is occurred, it should get printed into server log on terminal.

How to inspect the exception when email is triggered in letter_opener_email configuration?

As using puts, it can get printed, but where to write puts ?

using -

  ruby 1.9.3p0
  gem 'rspec-rails', '2.6.1.beta1'
  gem 'capybara', '2.1.0'
  gem 'letter_opener', '1.4.1'

Spec execution is done as -

rspec spec/features/user_spec.rb -fd --out log.txt

Requirement: when exception occurs, then it should get printed in Spec execution log(log.txt)

In my case, when exception occurs, it get saved into /letter_opener folder in html format.

I want to print that exception in log.txt in such a way that as it displays in browser(without displaying html, css tags).

letter_opener_files = Dir["#{Rails.root}/tmp/letter_opener//"]

letter_opener_files.sample is as below :

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
      <title> (ActiveRecord::StatementInvalid) "OCIError: ORA-01722: invalid number: SELECT SUM(\"CASH_BOOK\".\"DEBIT\") AS sum_id FR...</title>
    <style type="text/css">
    </style>
  </head>
  <body>
    <div id="container">
      <div id="message_headers">
        <dl>
          <dt>From:</dt>
          <dd>#&lt;Mail::Field:0x0000001dae4cb0&gt;</dd>          
            <dt>Subject:</dt>
            <dd><strong> (ActiveRecord::StatementInvalid) "OCIError: ORA-01722: invalid number: SELECT SUM(\"CASH_BOOK\".\"DEBIT\") AS sum_id FR...</strong></dd>
        </dl>
      </div>
        <p id="message_body">An ActiveRecord::StatementInvalid occurred in background at 2018-02-14 20:15:29 +1300 :
  OCIError: ORA-01722: 
  stmt.c:253:in oci8lib_191.so

  -------------------------------
Backtrace:
-------------------------------

  stmt.c:253:in oci8lib_191.so
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/ruby-oci8-2.1.2/lib/oci8/oci8.rb:474:in `exec'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:143:in `exec
  </p>

I have tried below solution which is working, but mail get printed(with tags which is not required) in console only, not in log.txt

describe 'User' do

  after(:each) do
    page = Nokogiri::HTML.parse(File.read(letter_opener_files.sample))
    puts "Exception Occurred - #{page.css('p')}"
  end

  it 'should not have any exception' do
    create(:user)
  end

end

Got Output as below :

1.9.3-p0 :347 >puts "Exception Occurred - #{page.css('p')}"

Exception Occurred - <p id="message_body">An ActiveRecord::StatementInvalid occurred in background at 2018-02-14 20:15:29 +1300 :

  OCIError: ORA-01722: invalid number:
  stmt.c:253:in oci8lib_191.so

  -------------------------------
Backtrace:
-------------------------------

  stmt.c:253:in oci8lib_191.so
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/ruby-oci8-2.1.2/lib/oci8/oci8.rb:474:in `exec'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:143:in `exec'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:627:in `block in exec_query'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.6/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.6/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'
</p>

Expecting Output(in log.txt & console) -

Exception Occurred - An ActiveRecord::StatementInvalid occurred in background at 2018-02-14 20:15:29 +1300 :

  OCIError: ORA-01722: invalid number:
  stmt.c:253:in oci8lib_191.so

  -------------------------------
Backtrace:
-------------------------------

  stmt.c:253:in oci8lib_191.so
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/ruby-oci8-2.1.2/lib/oci8/oci8.rb:474:in `exec'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb:143:in `exec'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-oracle_enhanced-adapter-1.4.1/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:627:in `block in exec_query'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.6/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /usr/local/rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.6/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'

Please advise if there is any better way to do this and how to print exception under log.txt?

rhunal
  • 395
  • 2
  • 15
  • It sounds like you've configured something else (exception_notifier maybe) to email all exceptions. You should just need to turn that off in the `test` environment. All of these gems are REALLY old though, you might want to look at upgrading at some point. – Thomas Walpole Feb 14 '18 at 20:38
  • Yes, but at this point I need to continue with exception_notifier. – rhunal Feb 15 '18 at 06:33
  • ok, but you don’t need to use it in the test environment – Thomas Walpole Feb 15 '18 at 06:44
  • @Thomas, but it works fine for my requirement. – rhunal Feb 15 '18 at 06:52
  • huh? If it worked for you then why post this question? exception_notifier is catching exceptions and mailing them into letter_opener - if you just disable exception_notifier in the test environment you’ll have the exception in you RSpec log just like you want – Thomas Walpole Feb 15 '18 at 06:55
  • Are you sure? what if exception occurs in background and rspec never fails? – rhunal Feb 15 '18 at 06:58
  • In what background? Any background jobs should be run online in test mode, or completion explicitly waited for in your tests. Disabling exception_notifier in test mode doesn’t stop exceptions from being reported, just from being emailed (which you shouldn’t want when running tests anyway) – Thomas Walpole Feb 15 '18 at 07:02

0 Answers0