Blog

Some Quick Tips for Tips for Testing Your Rails App

Placeholder Avatar
Mark Biegel
September 9, 2017

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:

ruby 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:

```ruby 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 34

Faker for Factories makes it easier when trying to create multiple records with unique validations.

E.g

Lets say we have a User model with unique 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 unique ```

``` #with faker FactoryGirl.define do factory :user do email { Faker::Internet.email } end end

FactoryGirl.create_list(:user, 25) # ‚úÖ üíµ ```