Inspector RSpec

look closely to find errors & wear goofy specs...

·

3 min read

Let's start this on a good note.

As mentioned in my last article, sending mail using a free SMTP provider in one of my nub projects is not possible.

Because of reasons that are multi-fold:

  1. New Rules - Dua Lipa.
  2. I slacked off.
  3. Not admiring to be a high-achiever; prefer delusion.

Thank you.

On that note, let's talk about testing your rails application. Perhaps you must have heard that testing is not so attractive or interesting to do just like I did or you know... delusion. But that might not be the case. I still wouldn't know cos I've only written basic tests for my app using Rspec.

So I will share what I know and how to write basic Rspec tests and feel bad as well. Y'all must be paying me.

Please refer to the website: RSpec library

If you want to get a very basic idea of what TDD - Test Driven Development is in Ruby please O' please solve Ruby Koans. It's fun and super-easy and highly informative excercise to learn Ruby.

Ruby Koans link: RubyKoans

My Ruby Koans Solution: My Solutions Repo Github

Let's write a very basic test for our Posts Controller.

Disclaimer: Understand that this is just to let you know how RSpec test are done and not be considered a how-to guide. Cos why would you!? Me nub. But I promise I will post much better guides in the near future once I am thorough in the said subject matter. Thank you.

In your *_spec.rb file:

require "rails_helper"

RSpec.describe PostsController, :type => :controller do
  describe "GET index" do
    it "has a 200 status code" do
      get :index
      expect(response.status).to eq(200)
    end
  end
end

It seems like it is not even proper code but it is. Rspec has many built-in methods/helpers to make testing that EZ.

For minimal quick-start reference: RSpec by RubyGuides

Few things to notice:

  1. describe refers to the action/request that is to be tested.
  2. it refers to the test code for the selected action :index here.
  3. The line expect(response.status).to eq(200) asserts for the response code to be '200'

This test is for Controller as in :type => :controller. We can write tests for Models, Controllers and even Views using Rspec library.

The above spec simply tests for the index action to return response code 200 and is active/working.

We could also write tests for Models, :type => :model. A simple example as show in: (relishapp.com/rspec/rspec-rails/v/5-1/docs/..)

require "rails_helper"

RSpec.describe Post, :type => :model do
  context "with 2 or more comments" do
    it "orders them in reverse chronologically" do
      post = Post.create!
      comment1 = post.comments.create!(:body => "first comment")
      comment2 = post.comments.create!(:body => "second comment")
      expect(post.reload.comments).to eq([comment2, comment1])
    end
  end
end

This spec simply tests for the comments to be displayed in order on reloading.

That's it for today. I will showcase sending a mail from my app in tomorrow's article and in the very near future write more on testing your Rails application. Thank you for reading!

Please subscribe to my newsletter!

Until then...

Love,

M.

liked_the_post == true ? comment and share : comment and share