Writing specs for OmniAuth using Rspec

Recently, I used omniauth to implement google login in my rails application. Integrating and getting omniauth to work is pretty easy, if you are facing problems with that, you can check out another blog post which talks about just that. Now that you have knowledge of how omniauth will work in your project, you might want to write some specs for the same!

Here is where it gets a little tricky, for you to write specs for omniauth you'll have to mock it and insert omniauth.hash in the request environment.

Open your rails_helper.rb and add the following code to it.

OmniAuth.config.test_mode = true omniauth_hash = { 'provider' => 'google_oauth2', 'uid' => '12345', 'info' => { 'name' => 'Tony Stark', 'email' => 'tony@stark.com', 'nickname' => 'Iron Man' }, 'extra' => {'raw_info' => { 'location' => 'New York', 'gravatar_id' => '123456789' } } } OmniAuth.config.add_mock(:google_oauth2, omniauth_hash)

Now whenever you need the omniauth hash in your specs, all you need to do is to call this line of code. After that your spec might look like this

before do
 request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:google_oauth2]

it 'should redirect to admin user' do
  get :create, :provider => :google_oauth2
  expect(response).to redirect_to(User.last)

Simple Delegator In Ruby

Writing decorators in Ruby is made easier by the SimpleDelegator. How SimpleDelegator works is best understood by an example

A PersonDecorator object can be created by passing in the Person object in the constructor. The SimpleDelegator will call the Person object's methods for the PersonDecorator.

The magic of calling the Person object's methods without defining it in the PersonDecorator lies in the Delegator class.

The SimpleDelegator is a child class of the Delegator class. The Delegator class overrides the method_missing method and calls the Person object's methods.


How to make Turbolinks play well with jquery in rails

Let's picture this, you have started making a Rails application and you're excited to use JavaScript/jQuery in it, but when you run the app your JavaScript only works if you have reloaded the page, and not if you're navigating to it from some other page in your app. Rails is shipped with a gem called Turbolinks which prevents your browser from loading the whole page every time you send a request, making them faster. Here is how they describe the gem on their GitHub page:

Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of) and the title in the head.

In a rails app, when you navigate from other page to your target page, instead of reloading the JavaScript again, it maintains the instance from the earlier page, and that's the reason why it doesn't work unless you reload your target page.

There are many workarounds to this problem, but the tidiest way I have come across is to add a gem called jQuery Turbolinks. Just add this line to your Gemfile and run bundle update

gem 'jquery-turbolinks'

then in your application.js file, require the javascripts in this particular order:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
// ... your other scripts here ...
//= require turbolinks

Now all you need to do is to restart your rails server, and you're good to go!

Sumit is an engineer at C42 Engineering. If you liked this post, please consider...


Reliable Code Coverage in rails With SimpleCov

A 100% code coverage doesn't ensure that your test suite is absolutely bulletproof. One of the reasons is that normally the controller specs are run, a significant amount of models are also covered, but we don't want that to reflect in our coverage results. SimpleCov, a complete code coverage suite which gives you options to filter, group, merge and format the results. We can also create custom Rake Tasks which will allow us to separately run the specs for our models, controllers and helpers.

Here's how you setup simplecov in your project:

gem 'simplecov', :require => false, :group => :test

Add the above line in your Gemfile and run bundle install, which will install the required gems. Now in your Rails project, open the rails_helper.rb file and add these lines above any other require statement.

Run your tests and run this command to view the report

$ open coverage/index.html

You can also configure simplecov by adding filters to it. You probably don’t want the spec or the config files to be included in your coverage results, here is how you do it.

You can also group your coverage results to be displayed in different tabs, like you can group controllers, models, views, helpers, etc.

The complete simplecov code snippet now looks like this:

Let's create a coverage.rake file under lib/tasks/ in your project and add these lines:

You can set environment variables and using those as the conditions you can run specific specs. Let's open up rails_helper.rb file and make these changes to your simplecov configuration.

add_group 'Models', "app/models"



add_group 'Controllers', "app/controllers"


Now head over to coverage.rake file, and write a rake task within namespace :coverage to exclusively run your model specs and its coverage.

Run the following command in terminal, followed by open coverage/index.html

$ rake spec:coverage:models

Similarly, you can also write another rake task to run your controller specs. If you haven't already, then try this out and let us know how much code you have got covered!

Sumit is an engineer at C42 Engineering. If you liked this post, please consider...


Tech Resume Writing 101 for Indian Grads

I've been working my way through hundreds for fresh grad resumes these last few months.

Sadly, most resumes I see actually hurt the candidate rather than help, which defeats the point of the resume and makes my life harder.

So here's some advice for fresh grads around resume building, especially if you're applying to a tech firm that actually cares about tech.

Your resume is an advertisement

Imagine your job is to go through dozens of ads every day. The ads are for different products in the same space, and you need to identify which of those products you're going to buy based on the ads.

This is what recruiters in every company do day after day, except your resume is that advertisement.

Don't bore them with a shitty ad identical in content to every other shitty ad they see.

Conformity is the enemy

The objective of your resume is to help you stand out from the crowd.

Trying to stand out by conforming to what your classmates or friends are doing is simply... stupid.

As I've said before, "Doing what most do leads to an average case outcome".

Your 'Objective' is bullshit

The objective seems to be a standard in most resumes, and is often extremely off-putting. This is awful given that it is the first thing about you that the recruiter sees.

Here's a typical objective:

To contribute my skills sets to the organization to achieve the goals and targets that enhance my professional and personal growth.

Mmm. Ok. You and everybody else.

A content-free objective section tells a recruiter is that you lack an objective.

Please take the time to carefully think this through - it's the first thing in your resume and sets the tone for everything that follows. I would go so far as to recommend writing a fresh, carefully crafted objective for each company you're applying to that speaks to that company's needs.

Academic performance

This section is often the only useful section in the resumes of most candidates.

Don't mess with this.

Prior art matters

This should be obvious: If you're applying to a tech startup, showcase your skills. Make sure you put all the code you've written front and centre.

Open source contributions, hobby projects and anything else that prove your ability to code result in an instant interview with us at C42, irrespective of academic performance.

Finally, this is 2015. A strong online presence is de rigueur for a tech resume. You should be active on Github, StackOverflow, Open Source User Groups, TopCoder and so on.

Extra curricular

I just went through ~20 resumes from a batch where everyone had exactly the same extra curricular activities.

Stop copy-pasting. It is hurting you.

Extra curricular activities help build up a more complete picture of you, so they are useful, but please put successes at the top and give them attention.

Please also try to avoid citing achievements from your fourth standard painting contest.

The achievements must be significant, recent or (ideally) both.

If you've succeeded in tech related activities, please highlight these.


If your goal is to get a prospective employer's attention through your resume, don't do it by making your resume look like everyone else's.

This doesn't mean changing what colours you use. It means understanding that you're making an advertisement - a tasteful, understated one, but an advertisement nonetheless.

Sidu is a Partner at C42 Engineering. If you liked this post, please consider...