RSpec cheatsheet install rspec-rails gem only, it will install all the rspec related gems. Basic setup steps Step1: gem 'rspec-rails' root@f79191f58a4f:~# gem list rspec *** LOCAL GEMS *** rspec-core (3.9.3) rspec-expectations (3.9.4) rspec-mocks (3.9.1) rspec-rails (3.9.1) rspec-support (3.9.4) Step2: rails generate rspec:install it will create 3 files, .rspec spec/rails_helper.rb spec/spec_helper.rb Step3: put these two lines in the .rspec file, --require spec_helper --format documentation Step4: run rspec as, rspec ------ Basic knowledge All the spec(specification) files stay inside spec folder. Allthe spec file ends with *_spec.rb Example - spec/sales_spec.rb All files in this directory(spec/**/*_spec.rb)will be run automatically when we run, rspec Dry run spec as, rspec --dry-run Run a particular spec, rspec spec/sales_spec.rb Run a particular spec from a particular line, rspec spec/coffee_spec.rb:31 Run rspec with formatted output as, rspec --format doc...
In the Rails do not prefer to use Time.now, Date.today etc We should always prefer to use it with zone like, Time.zone.now, Time.zone.today etc. but why so? Beacause when we fetch Time.now its always return system local time and ignore config.time_zone set in the application.rb. file. Here is the example tested in the rails console, 3.0.3 :001 > Time.now => 2022-04-19 09:57:58.413034249 +0530 3.0.3 :002 > Time.zone => #<ActiveSupport::TimeZone:0x000055704feec360 @name="UTC", @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=nil> 3.0.3 :003 > Time.zone.now => Tue, 19 Apr 2022 04:28:07.277000358 UTC +00:00 3.0.3 :004 > Time.zone = 'Sydney' => "Sydney" 3.0.3 :005 > Time.zone => #<ActiveSupport::TimeZone:0x000055705192d850 @name="Sydney", @tzinfo=#<TZInfo::DataTimezone: Australia/Sydney>, @utc_offset=nil> 3.0.3 :008 > Time.now => 2022...
Comments
Post a Comment
Let me know for any query..