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, 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.
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