Testing 1, 2, 3... RSpec Basics and Tips for RoR.
Tags: ruby, rails, testing
Published: 2021-05-16 20:34:29

I have recently begun my dive into the job search pool and have landed a couple of interviews. Exciting, right?! In each interview I have been asked about testing. Whether it was in regards to my preference in testing or actually having to implement my own tests for a challenge, the subject came up. That's when I realized, my way of testing an application (which had been by running my server and testing pieces manually) would not be the best route when in the real software engineering world and dealing with much LARGER applications. Now don't get me wrong, I have definitely read tests before, I've just never really written them out myself until I had a code challenge which asked for such to be done. So I thought it would be a great idea to take a dive into some basic keywords to read and write tests as well as compile a small cheatsheet of RSpec matchers. 

First and foremost, you want to ensure you have the rspec gem in your gem file and run `bundle install`. You may also generate boilerplate files with the `rails generate` command.
```
gem 'rspec-rails', '~> 5.0.0'

rails g rspec:install
```  

Next, we'll need to require any and all files we want to include in the specific test file we're working in. 
**If you use scaffold to generate your files, your tests will automatically `require 'rails_helper'`.**

```
require 'main/tester.rb'
```


###Now the fun stuff. Actually writing tests!


To have a skeleton of a test, create a `describe` block. The `describe` keyword tells RSpec which class and/or string argument you want to test as well as allow you to group tests for that class/argument together. Within your `describe` block you will also have an `it` block which will define a test/test case. Like `describe`, `it` accepts class names and string arguments (describe what behavior should happen inside the block). Another important keyword is `expect` which is a verification step to ensure the condition we expected has been met. 

```
describe Tester do
  it "does something" do
   test = Tester.new
   expect(test.name).to eq("something")
  end
end
```


###Let


Alright, `let`'s get down to business! _(Extra cool points if you understood that reference)_ RSpec has a `let` method which allows you to reuse objects for many tests. The `let` method is essentially creating a variable to be used throughout the specific test file. 

```
let(:valid_attributes) {
# here you will add a hash of your valid attributes
  {
  first_attr: "your first value",
  second_attr: "make sure it's the correct datatype",
  another_attr: true,
  and_so_on: 2
  }
}
```

Now, if you need to create an object throughout, you can simply write `Tester.create valid_attributes` in your describe block. 


###Subject


Another version of `let` is the `subject` keyword where you can only have one subject (an instance) of the object you're testing.

```
describe "running a test with subject" do 
  subject { Tester.new }
  # or subject (:first_test) { Tester.new }
  it "was saved successfully" do
    expect(subject).to be_an(Tester)
    # or expect(first_test).to be_an(Tester)
  end
end
```

**The `subject` keyword is normally used to test ActiveRecord validations with shoulda matchers.**


###Context


If you find yourself wanting or needing to test different scenarios within your application `context` is the tool for you. With the `context` keyword you can describe different possibilities to be tested within that file. 

```
describe Tester do
  context "when user is a dog owner" do
    it "displays some dog info"
    end

    it "displays dog toy info" do
    end
  end

  context "when user is a cat owner" do
    it "displays some cat info"
    end

    it "displays cat toy info" do
    end
  end
end
```


Now that I've reviewed the basics of creating your own RSpec tests and what the keywords mean and do, I'll provide you with a small cheatsheet of matchers I've compiled.

```
.to eq
.not_to be
.to be_success
.to have_http_status
.to be_a_new
.to be_an
.to be_a
.to render_template
.to route_to
.to redirect_to
.to be_redirect
.to be_routable
.to be_nil
.to start_with
.to be_between
.to end_with
.to have_attributes
.to raise_error
.to change
.to be_instance_of
```


I hope this blog helps you understand how to read and write RSpec tests a tad more. I know writing it certainly helped me! 


Resources:
* https://www.rubyguides.com/2018/07/rspec-tutorial/
* https://github.com/rspec/rspec-rails
* https://relishapp.com/rspec/rspec-core/v/3-10/docs/example-groups/basic-structure-describe-it#one-group,-one-example