[RUBY] I rewrote the Rails tutorial test with RSpec

Nice to meet you! My name is k @ kuni. Now that you've read Everyday Rails-Introduction to Rails Testing with RSpec (https://leanpub.com/everydayrailsrspec-jp), I'd like to rewrite the test code for the Ruby on Rails Tutorial (https://railstutorial.jp/) with RSpec as its output. I would be grateful if you could point out any points that do not reach the code.

First setup

First, let's clone the repository. https://github.com/mhartl/sample_app_6th_ed

$ git clone https://github.com/mhartl/sample_app_6th_ed.git
$ cd sample_app_6th_ed
$ bundle install --witout 'production'
$ rails db:migrate

Next is the RSpec setup. Please refer to the article I wrote before. https://qiita.com/OSouhei/items/156a4d5729172b9acf3a

static_pages_controller_test First, let's fix it from static_pages_controller_test!

The original test code looks like this

test/controllers/static_pages_controller_test.rb


require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get root_path
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get help_path
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get about_path
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end

Currently, the controller spec seems to be deprecated, so this time I would like to write using the request spec! Execute the following command to create the request spec.

$ rails g rspec:request static_pages

The specs I wrote are like this

spec/requests/static_pages_request_spec.rb


require 'rails_helper'

RSpec.describe "StaticPages", type: :request do
  context "#home" do
    it "responds successfully" do
      get root_path
      expect(response).to have_http_status "200"
      #Is it better to test with features?
      expect(response.body).to include "Ruby on Rails Tutorial Sample App"
    end

    # site_layout_Migrate from spec
    it "render static_pages/home" do
      get root_path
      expect(response).to render_template :home
    end
  end

  context "#help" do
    it "responds successfully" do
      get help_path
      expect(response).to have_http_status "200"
      expect(response.body).to include "Help | Ruby on Rails Tutorial Sample App"
    end
  end

  context "#about" do
    it "responds successfully" do
      get about_path
      expect(response).to have_http_status "200"
      expect(response.body).to include "About | Ruby on Rails Tutorial Sample App"
    end
  end

  context "#contact" do
    it "responds successfully" do
      get contact_path
      expect(response).to have_http_status "200"
      expect(response.body).to include "Contact | Ruby on Rails Tutorial Sample App"
    end
  end
end

It may be better to write in the feature specs whether the page has the correct title. For example, call it like this.

expect(page).to have_title "Ruby on Rails Tutorial Sample App"

site_layout_test

Next, we will rewrite site_layout_test. Originally like this

test/integration/site_layout_test.rb


require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
  end
end

The code I wrote looks like this

spec/features/site_layout_test.rb


require 'rails_helper'

RSpec.feature "SiteLayouts", type: :feature do
  it "has links" do
    visit root_path
    #Check the request specifications to see if the correct template is rendered
    expect(page).to have_link nil, href: root_path, count: 2
    expect(page).to have_link "Help", href: help_path
    expect(page).to have_link "About", href: about_path
    expect(page).to have_link "Contact", href: contact_path
  end
end

It's not defined when trying to use the variable response in the feature specs! I got an error and got angry, so I moved to the request spec to see if the correct template was rendered.

in conclusion

Thank you for reading until the end! When I first saw RSpec, the code was hard to read, and I found it complicated and inconvenient to use the factory. (I still find the factory difficult ...) However, recently, writing with RSpec makes the output beautiful, using context etc., the test code itself is organized, and the prepared matchers are intuitive and easy to understand, and I'm gradually becoming fond of it. .. I'm off the topic, but when I have time, I'll be doing the rest of the tests, so thank you!

Recommended Posts

I rewrote the Rails tutorial test with RSpec
[Rails] Test with RSpec
Let's unit test with [rails] Rspec!
Rails tutorial test
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Understand code coverage with Rspec, the Ruby on Rails test framework
I want to test Action Cable with RSpec test
[Rails] Learning with Rails tutorial
Test Nokogiri with Rspec.
[Rails] I want to test with RSpec. We support your step [Introduction procedure]
I changed the way Rails tutorials run: Rails Tutorial Notes-Chapter 9
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails] I tried playing with the comment send button
Test Active Strage with RSpec
I tried the Docker tutorial!
[Rails] Test code using Rspec
I tried the VueJS tutorial!
Test GraphQL resolver with rspec
[Rails] About Rspec response test
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
Test with RSpec + Capybara + selenium + chromedriver
[Rails] I tried deleting the application
[Rails5] Rspec -Unit test when nesting-
Copy and paste test with RSpec
Follow the Datomic tutorial with Datascript
I made a reply function for the Rails Tutorial extension (Part 1)
I tried to implement the image preview function with Rails / jQuery
I got a warning message with the rails _6.0.3_ new hello_myapp command
I implemented Rails API with TDD by RSpec. part2 -user authentication-
I made a reply function for the Rails Tutorial extension (Part 5):
I implemented Rails API with TDD by RSpec. part3-Action implementation with authentication-
With EqualsVerifier, the equals () test was instant
rails tutorial
Prepare the format environment with "Rails" (VScode)
I implemented Rails API with TDD by RSpec. part1-Action implementation without authentication-
rails tutorial
rails tutorial
rails tutorial
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the Spring-Batch test
Check the processing contents with [rails] binding.pry
Learn RSpec with Everyday Rails. Until you bundle install the sample app.
rails tutorial
rails test fails with database reference error
rails tutorial
I want to introduce the committee with Rails without getting too dirty
rails tutorial
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
I don't see the flash message with redirect_to
Where I got stuck in today's "rails tutorial" (2020/10/08)
[Rails] When I use form_with, the screen freezes! ??
[Ruby on Rails] Until the introduction of RSpec
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[For beginners] Test devise user registration with RSpec
Introducing Rspec with Ruby on Rails x Docker
I tried to organize the session in Rails
I tried writing CRUD with Rails + Vue + devise_token_auth
Publish the app made with ruby on rails
[NCMB] I searched the data store with mbaas