Why does strptime behave differently in Rails' test environment? - ruby-on-rails

I'm trying to parse the date 2/31/2019, which should raise an error.
In the Rails console this works as expected in development mode, but my tests are failing in test mode because it returns March 3, 2019.
Date.strptime("2/31/2019", "%m/%d/%Y") #=> dev env: error
Date.strptime("2/31/2019", "%m/%d/%Y") #=> test env: “Sun, 03 Mar 2019”
The commands I'm using are:
rails c
RAILS_ENV=test rails c
rspec <filename>
I'm using Ruby 2.3.1 and Rails 5.0.6.

Timecop overwrites the Date.strptime definition:
[4] pry(main)> puts Date.method(:strptime).source
def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY)
unless start == Date::ITALY
raise ArgumentError, "Timecop's #{self}::#{__method__} only " +
"supports Date::ITALY for the start argument."
end
Time.strptime(str, fmt).to_date
end
That's why the output is different between environments, since Timecop is only required in test.

Related

Is there any way to set a break point for debug in the environment in which gems like binding.pry are not installed?

From this April, I participated in a new project in which gems for debug are not installed and I am not allowed to install new ones.
I would like to set a break point and see the inside of variables like binding.pry.
I googled to debug in such a way without newly installing gems or by using extensions of VScode, but most of the pages require me to install ones.
It would be highly appreciated if you would share with me your ideas to debug in the way.
== Additional Information on April 8th, 2020 ==
Ruby version is 1.8.7.
Rails version is 2.3.5.
Unfortunately, Ruby doesn't really have a built-in breakpoint-based debugger in that sense.
The closest thing you can do is start an IRB session mid-program, which halts it and allows you to inspect the current state of the program by evaluating Ruby commands. This is available in Ruby 2.4 and above as binding.irb. (There are docs available for this on the Binding class.)
IRB doesn't let you step forwards like Pry with Pry-Nav does, but you can just insert binding.irb expressions in as many places as you need, and exit from IRB to "step" to the next one.
Take ths simple program. I want to check the value of those three variables I've assigned, so I put binding.irb there:
puts "=== Hello! ==="
a = 1
b = 2
c = 3
binding.irb
puts "=== Goodbye! ==="
Upon running this program, Ruby will give you an IRB prompt after it's evaluated c = 3, where you can run Ruby code to check values and whatnot (type exit to leave):
=== Hello! ===
From: test.rb # line 7 :
2:
3: a = 1
4: b = 2
5: c = 3
6:
=> 7: binding.irb
8:
9: puts "=== Goodbye! ==="
irb(main):001:0> a
=> 1
irb(main):002:0> b
=> 2
irb(main):003:0> c
=> 3
irb(main):004:0> exit
=== Goodbye! ===

Disable rest-client output on minitest

I've searched for this, but without success.
I have some tests (minitest) that use RestClient and Webmock. When passing for those tests I always have the request logged, polluting the test ouput:
[$] rake
Run options: --seed 60435
Running:
.........................................................RestClient.get "http://example.com/some_controller/some_action?userLocale=fr_FR", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client/2.0.0 (darwin14.1.0 x86_64) ruby/2.2.1p85" # => 200 OK | 4 bytes
Is there a way to disable this ?
[EDIT]
Just to add, if I call the same address using ruby URL I have nothing logged (even using webmock) so it really is something related with Rest-client.
I already tried to set the ENV['RESTCLIENT_LOG'] variable, but without success.
What about:
RestClient.stub :log, '' do
# Your test code here
end
http://www.rubydoc.info/gems/minitest/4.2.0/Object:stub
You have many other options to redirect the log output:
In your test_helper.rb:
RestClient.log = 'tmp/test.log'
http://www.rubydoc.info/gems/rest-client/1.8.0/RestClient.log=
From the command line:
RESTCLIENT_LOG=tmp/restclient.log bundle exec rails test
In last resort you could monkey patch:
# test_helper.rb
RestClient.class_eval do
def self.log
''
end
end

Why is YAML.safe_load failing on a YAML alias?

I've a locale file in my Rails application that works fine with Rails, but when I tried to use it with react_on_rails rake task (rake react_on_rails:locale) I'm getting this error:
Psych::BadAlias: Unknown alias: item_attributes
I found that the rake task is basically calling YAML.safe_load, so I prepared the simplest example without Rails and the error is still there. Here's the sample Ruby script:
require 'yaml'
YAML.safe_load(File.open('test.yml'))
And here's the test.yml file (shorter version of the actual locale file):
pl:
language: Polski
dictionary_name: simple
activerecord:
attributes:
line_item: &item_attributes
variant: Produkt
quantity: Ilosc
price: Cena Netto
total_price: Wartosc Netto
vat_rate: VAT
total_vat_amount: Kwota VAT
total_gross_price: Wartosc Brutto
order_item:
<<: *item_attributes
I'm still getting the error:
/usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:402:in `visit_Psych_Nodes_Alias': Unknown alias: item_attributes (Psych::BadAlias)
Any ideas why this works fine with Rails but fails here? Any other way to avoid duplication in the YAML file and make it work for both Rails and YAML.safe_load?
I found an answer in the Psych documentation, at https://ruby-doc.org/stdlib-2.1.0/libdoc/psych/rdoc/Psych.html.
Aliases can be explicitly allowed by changing the aliases parameter. For example:
x = []
x << x
yaml = Psych.dump x
Psych.safe_load yaml # => raises an exception
Psych.safe_load yaml, [], [], true # => loads the aliases
Whether to allow aliases or not is a boolean passed to safe_load as the fourth argument.

Rails 3.1 rake test:profile deprecation warning and empty output files

When runnign rake test:profile I get:
DEPRECATION WARNING: read_csv_fixture_files is deprecated and will be
removed from Rails 3.2. (called from block in autorun at
/Users/hade/.rvm/rubies/ruby-1.9.2-p290-perf/lib/ruby/1.9.1/minitest/unit.rb:508)
Unit.rb around line 508:
def self.autorun
at_exit {
next if $! # don't run if there was an exception
# the order here is important. The at_exit handler must be
# installed before anyone else gets a chance to install their
# own, that way we can be assured that our exit will be last
# to run (at_exit stacks).
exit_code = nil
at_exit { exit false if exit_code && exit_code != 0 }
exit_code = MiniTest::Unit.new.run(ARGV) <------ LINE 508
} unless ##installed_at_exit
##installed_at_exit = true
end
Is MiniTest calling this read_csv_fixture_files method? Is this normal behaviour with my setup? How can I remove this?
My environment:
Rails 3.1.0
Ruby 1.9.2-p290 with GC-patch
My tests seem to run ok, but my output files are useless. In my output files I only have this line:
ActiveSupport::Testing::Performance::Profiler#run
But when I run rails profiler 'User.all' I get nice console output and output files with some content.
Any help would be really appreciated. I have tried different Ruby versions with GC-patch, but no help there.

discrepancy between requiring a path in rails console vs. rails s (WeBRICK)

I'm using Machinist blueprints in development.
from development.rb:
config.after_initialize do
require 'spec/support/blueprints'
puts "********* blueprints loaded! *********"
end
it works fine in the console.
michael-schwabs-macbook-pro:medtext mschwab$ rails c
********* blueprints loaded! *********
Loading development environment (Rails 3.0.7)
irb(main):001:0> d = Doctor.make
=> #<Doctor id: 101, first_name: nil, ....
When I run the server, my controllers know that my models respond to #make, but they don't know that the blueprints are defined.
(rdb:70) Doctor.respond_to?(:make)
true
(rdb:70) Doctor.make
RuntimeError Exception: No blueprint for class Doctor
This is odd because the statement
require 'machinist/active_record'
is in the blueprints.rb file. Also, the "loaded!" statement gets printed out in my server log.
=> Ctrl-C to shutdown server
********* blueprints loaded! *********
=> Debugger enabled

Resources