Some Quick Tips for Tips for Testing Your Rails App
RSpec Mocks
by Leo Liang
Environment Variables
There might be times when you need to mock a certain value for ENV without overriding other values. This can be easily achieved as follows:
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('REPORT_ID').and_return('5707702298738688')
end
This will only stub ENV[‘REPORT_ID’]
and return value for all the other env vars.
Date.today
Normally we use Timecop
to set dates. But you need to remember to run Timecop.return
afterwards to avoid any unexpected behaviour:
describe "some set of tests to mock" do
before do
Timecop.freeze(Time.local(1990))
end
after do
Timecop.return
end
it "should do blah blah blah" do
end
end
Using Faker for Factories
by Sameera Gayan
Faker for Factories makes it easier when trying to create multiple records with uniq validations.
E.g
Lets say we have a User model with uniq email.
#without using Faker
FactoryGirl.define do
factory :user do
email "sameera@reinteractive.net"
end
end
FactoryGirl.create_list(:user, 25) # this will fail as the email us not uniq
#with faker
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
end
end
FactoryGirl.create_list(:user, 25) # ✅ 💵
Latest Articles by Our Team
Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.
-
No app left behind: Upgrade your application to Ruby 3.0 and s...
-
A look forward from 2020
-
Testing Rails applications on real mobile devices (both design...
We Hire Only the Best
reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.
We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.
Free Community Workshops
We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.