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>#<Mail::Field:0x0000001dae4cb0></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
?