Slide 1

Slide 1 text

¡hola! !

Slide 2

Slide 2 text

Secrets of Testing Rails 5 apps

Slide 3

Slide 3 text

Prathamesh Sonpatki @_cha1tanya

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Pune, India

Slide 6

Slide 6 text

Pune, India

Slide 7

Slide 7 text

Rails issues team

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Slide 10

Slide 10 text

I don’t have stickers

Slide 11

Slide 11 text

Stickers

Slide 12

Slide 12 text

Do you have stickers?

Slide 13

Slide 13 text

T-Shirts are fine too

Slide 14

Slide 14 text

This talk is not about RSpec

Slide 15

Slide 15 text

It is about default Rails test suite

Slide 16

Slide 16 text

Secrets of Testing Rails 5 apps

Slide 17

Slide 17 text

Delete tests

Slide 18

Slide 18 text

Delete tests

Slide 19

Slide 19 text

Secrets of Testing Rails 5 apps

Slide 20

Slide 20 text

Running tests

Slide 21

Slide 21 text

rake test Rails 4

Slide 22

Slide 22 text

Rerun snippets ❌ Run tests by line ❌ Fail fast ❌ Rails 4

Slide 23

Slide 23 text

Rerun snippets ❌ Run tests by line ❌ Fail fast ❌ Coloured output ❌ Rails 4

Slide 24

Slide 24 text

Running tests without batteries

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Let’s make runing tests in Rails great again

Slide 28

Slide 28 text

https://github.com/rails/rails/pull/18596

Slide 29

Slide 29 text

Test Runner

Slide 30

Slide 30 text

bin/rails test Rails 5

Slide 31

Slide 31 text

$ bin/rails t -h minitest options: -h, --help Display this help. -s, --seed SEED Sets random seed. Also via env. Eg: SEED=n rake -v, --verbose Verbose. Show progress processing files. -n, --name PATTERN Filter run on /regexp/ or string. Known extensions: rails, pride Usage: bin/rails test [options] [files or directories] You can run a single test by appending a line number to a filename: bin/rails test test/models/user_test.rb:27 You can run multiple files and directories at the same time: bin/rails test test/controllers test/integration/login_test.rb By default test failures and errors are reported inline during a run. Rails options: -e, --environment ENV Run tests in the ENV environment -b, --backtrace Show the complete backtrace -d, --defer-output Output test failures and errors after the test run -f, --fail-fast Abort test run on first failure or error -c, --[no-]color Enable color in the output Rails 5

Slide 32

Slide 32 text

$ bin/rails t -h minitest options: -h, --help Display this help. -s, --seed SEED Sets random seed. Also via env. Eg: SEED=n rake -v, --verbose Verbose. Show progress processing files. -n, --name PATTERN Filter run on /regexp/ or string. Known extensions: rails, pride Usage: bin/rails test [options] [files or directories] You can run a single test by appending a line number to a filename: bin/rails test test/models/user_test.rb:27 You can run multiple files and directories at the same time: bin/rails test test/controllers test/integration/login_test.rb By default test failures and errors are reported inline during a run. Rails options: -e, --environment ENV Run tests in the ENV environment -b, --backtrace Show the complete backtrace -d, --defer-output Output test failures and errors after the test run -f, --fail-fast Abort test run on first failure or error -c, --[no-]color Enable color in the output Rails 5

Slide 33

Slide 33 text

$ bin/rails t -h minitest options: -h, --help Display this help. -s, --seed SEED Sets random seed. Also via env. Eg: SEED=n rake -v, --verbose Verbose. Show progress processing files. -n, --name PATTERN Filter run on /regexp/ or string. Known extensions: rails, pride Usage: bin/rails test [options] [files or directories] You can run a single test by appending a line number to a filename: bin/rails test test/models/user_test.rb:27 You can run multiple files and directories at the same time: bin/rails test test/controllers test/integration/login_test.rb By default test failures and errors are reported inline during a run. Rails options: -e, --environment ENV Run tests in the ENV environment -b, --backtrace Show the complete backtrace -d, --defer-output Output test failures and errors after the test run -f, --fail-fast Abort test run on first failure or error -c, --[no-]color Enable color in the output Rails 5

Slide 34

Slide 34 text

Powered by Minitest 5

Slide 35

Slide 35 text

Inspirations

Slide 36

Slide 36 text

RSpec mintiest-reporters maxitest m and others &

Slide 37

Slide 37 text

Writing tests

Slide 38

Slide 38 text

Unit tests Functional tests Integration tests

Slide 39

Slide 39 text

Unit tests Functional tests Integration tests

Slide 40

Slide 40 text

Controller tests

Slide 41

Slide 41 text

What do we test in a controller test

Slide 42

Slide 42 text

request —> response

Slide 43

Slide 43 text

request —> response

Slide 44

Slide 44 text

class ArticlesControllerTest < ActionController::TestCase test "should create article" do assert_difference('Article.count') do post :create, article: { title: @article.title } end assert_redirected_to article_path(assigns(:article)) end end Rails 4

Slide 45

Slide 45 text

class ArticlesControllerTest < ActionController::TestCase test "should create article" do assert_difference('Article.count') do post :create, article: { title: @article.title } end assert_redirected_to article_path(assigns(:article)) end end Rails 4

Slide 46

Slide 46 text

class ArticlesControllerTest < ActionController::TestCase test "should create article" do assert_difference('Article.count') do c end assert_redirected_to article_path(assigns(:article)) end end Rails 4

Slide 47

Slide 47 text

class ArticlesControllerTest < ActionController::TestCase test "should create article" do assert_difference('Article.count') do post :create, article: { title: @article.title } end assert_redirected_to article_path(assigns(:article)) end end Rails 4

Slide 48

Slide 48 text

request —> state

Slide 49

Slide 49 text

Controller tests are easy

Slide 50

Slide 50 text

Controller tests are easy to write

Slide 51

Slide 51 text

Integration tests

Slide 52

Slide 52 text

class ArticlesIntegrationTest < ActionDispatch::IntegrationTest test "should create article" do assert_difference('Article.count') do post articles_url, params: { article: { title: @article.title } } end assert_redirected_to article_path(Article.last) end end

Slide 53

Slide 53 text

class ArticlesIntegrationTest < ActionDispatch::IntegrationTest test "should create article" do assert_difference('Article.count') do post articles_url, params: { article: { title: @article.title } } end assert_redirected_to article_path(Article.last) end end

Slide 54

Slide 54 text

Almost same as controller tests

Slide 55

Slide 55 text

# Functional controller test post :create, article: { title: @article.title } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4

Slide 56

Slide 56 text

# Functional controller test post :create, article: { title: @article.title } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4

Slide 57

Slide 57 text

# Functional controller test post :create, article: { title: @article.title } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4

Slide 58

Slide 58 text

Close to Real World

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

https://github.com/eileencodes/ integration_performance_test

Slide 61

Slide 61 text

Integration controller tests are default in Rails 5

Slide 62

Slide 62 text

require 'test_helper' class UsersControllerTest < ActionDispatch::IntegrationTest setup do @user = users(:one) end test "should get index" do get users_url assert_response :success end ………… end Rails 5

Slide 63

Slide 63 text

Functional controller tests are soft deprecated

Slide 64

Slide 64 text

Old functional style tests will continue to work for now*****

Slide 65

Slide 65 text

Might be extracted in a separate gem in 5.1

Slide 66

Slide 66 text

1. Prefer ActionDispatch::IntegrationTest for new Rails 5 apps. 2. Leave functional controller tests alone for upgraded Rails 4 apps.

Slide 67

Slide 67 text

Implications

Slide 68

Slide 68 text

class ArticlesControllerTest < ActionController::TestCase setup do john = users(:john) session[:user_id] = john.id end end Rails 4

Slide 69

Slide 69 text

class ArticlesControllerTest < ActionController::TestCase setup do john = users(:john) session[:user_id] = john.id end end Rails 4

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do john = users(:john) session[:user_id] = john.id end end NoMethodError: undefined method `session' for nil:NilClass Rails 5

Slide 72

Slide 72 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do john = users(:john) post login_url, params: { email: john.email, password: john.password } end Rails 5

Slide 73

Slide 73 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest def sign_in_as(user) post login_url, params: { email: user.email, password: user.password } end setup do john = users(:john) sign_in_as john end test ‘john can access ..’ Rails 5

Slide 74

Slide 74 text

class ArticlesControllerTest < ActionController::TestCase test ‘index’ do @request.headers["HTTP_REFERER"] = “http://example.com" get :index end end Rails 4

Slide 75

Slide 75 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest test ‘index’ do @request.headers["HTTP_REFERER"] = “http://example.com" get posts_url end end # NoMethodError: undefined method `headers' for nil:NilClass Rails 5

Slide 76

Slide 76 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest test ‘index’ do get posts_url, headers: { “HTTP_REFERER” => ”example.com” } end end Rails 5

Slide 77

Slide 77 text

Black Box

Slide 78

Slide 78 text

What not to test in integration style controller tests?

Slide 79

Slide 79 text

Don’t test controller internals

Slide 80

Slide 80 text

test "repos" do get :index assert_response :success, 'successfully renders index’ assert_not_nil assigns(:repos), 'assigns to repos' assert_template :index, 'render index template' end Rails 4

Slide 81

Slide 81 text

test "repos" do get :index assert_response :success, 'successfully renders index’ assert_not_nil assigns(:repos), 'assigns to repos' assert_template :index, 'render index template' end Rails 4

Slide 82

Slide 82 text

Testing the Controller-View interface

Slide 83

Slide 83 text

test "repos" do get :index assert_response :success, 'successfully renders index’ assert_select ".repo-item-title", text: 'issue_triage_sandbox' end Rails 5

Slide 84

Slide 84 text

test "repos" do get :index assert_response :success, 'successfully renders index’ assert_select ".repo-item-title", text: 'issue_triage_sandbox' end #=> Repo.first.name = ‘issue_triage_sandbox’ Rails 5

Slide 85

Slide 85 text

gem ‘rails-dom-testing’

Slide 86

Slide 86 text

End to end testing

Slide 87

Slide 87 text

Controller - View interface

Slide 88

Slide 88 text

Just an implementation detail

Slide 89

Slide 89 text

Deprecated in Rails 5 assert_template assigns

Slide 90

Slide 90 text

BUT

Slide 91

Slide 91 text

Testing controller isolated from views is needed**

Slide 92

Slide 92 text

gem ‘rails-controller-testing’

Slide 93

Slide 93 text

HTTP Request test helpers

Slide 94

Slide 94 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, product: { name: "FIFA" } assert_response :success end end Rails 4

Slide 95

Slide 95 text

Positional arguments Rails 4

Slide 96

Slide 96 text

1. An optional hash of request parameters 2. An optional hash of session variables 3. An optional hash of flash values

Slide 97

Slide 97 text

1. An optional hash of request parameters 2. An optional hash of session variables 3. An optional hash of flash values

Slide 98

Slide 98 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, { product: { name: "FIFA" } }, { user_id: User.first.id } assert_response :success end end Rails 4

Slide 99

Slide 99 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, { product: { name: "FIFA" } }, { user_id: User.first.id }, { notice: ‘I don’t know what I am doing’ } assert_response :success end end Rails 4

Slide 100

Slide 100 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, nil, { user_id: User.first.id }, { notice: ‘I don’t know what I am doing’ } assert_response :success end end Rails 4

Slide 101

Slide 101 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, nil, nil, { notice: ‘I don’t know what I am doing’ } assert_response :success end end Rails 4

Slide 102

Slide 102 text

Rails 4

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

Ruby 2.2.2+

Slide 105

Slide 105 text

Keyword arguments

Slide 106

Slide 106 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, product: { name: "FIFA" } assert_response :success end end Warning, passing keyword arguments is required. Rails 5

Slide 107

Slide 107 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, params: { product: { name: "FIFA" } } assert_response :success end end Rails 5

Slide 108

Slide 108 text

ActionController::TestCase #=> params flash session xhr Rails 5

Slide 109

Slide 109 text

ActionDispatch::IntegrationTest #=> params env headers xhr Rails 5

Slide 110

Slide 110 text

Implications

Slide 111

Slide 111 text

One of the pain points of upgrading to Rails 5

Slide 112

Slide 112 text

Change all existing tests to get rid of warning

Slide 113

Slide 113 text

Missing docs for ActionController TestCase

Slide 114

Slide 114 text

Rewriting positional args as keyword args

Slide 115

Slide 115 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, session: { name: "FIFA" } assert_response :success end end Rails 4

Slide 116

Slide 116 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create, session: { name: "FIFA" } assert_response :success end end session is interpreted as session not as params. Rails 5

Slide 117

Slide 117 text

Minor incompatibilities

Slide 118

Slide 118 text

class ProductsControllerTest < ActionController::TestCase def test_update raw_string = "{title:\"sample\"}" patch :update, raw_string, id: 42 assert_response :success end end Rails 4

Slide 119

Slide 119 text

class ProductsControllerTest < ActionController::TestCase def test_update raw_string = "{title:\"sample\"}" patch :update, raw_string, id: 42 assert_response :success end end # No route matches {:action=>"update", :controller=>"posts"} Rails 5

Slide 120

Slide 120 text

class ProductsControllerTest < ActionController::TestCase def test_update raw_string = "{title:\"sample\"}" request.env['RAW_POST_DATA'] = raw_string patch :update, id: 42 assert_response :success end end # Works Rails 5

Slide 121

Slide 121 text

gem ‘rails/rails’, branch: ‘5-0-stable’

Slide 122

Slide 122 text

Testing API requests

Slide 123

Slide 123 text

JSON or XML requests

Slide 124

Slide 124 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } } end end end

Slide 125

Slide 125 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path(format: :json), params: { article: { title: 'Ahoy!' } }.to_json, headers: { 'Content-Type' => 'application/json' } end end end Rails 4

Slide 126

Slide 126 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path(format: :json), params: { article: { title: 'Ahoy!' } }.to_json, headers: { 'Content-Type' => 'application/json' } end end end Rails 4

Slide 127

Slide 127 text

Request Encoding

Slide 128

Slide 128 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json end end end Rails 5

Slide 129

Slide 129 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json end end end Rails 5

Slide 130

Slide 130 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json end assert_equal({ 'id' => Article.last.id, 'title' => 'Ahoy!' }, parse_json(response.body)) end end Rails 4

Slide 131

Slide 131 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json end assert_equal({ 'id' => Article.last.id, 'title' => 'Ahoy!' }, response.parsed_body) end end Rails 5

Slide 132

Slide 132 text

require 'test_helper' class ApiTest < ActionDispatch::IntegrationTest test 'creates articles' do assert_difference -> { Article.count } do post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json end assert_equal({ 'id' => Article.last.id, 'title' => 'Ahoy!' }, response.parsed_body) end end Rails 5

Slide 133

Slide 133 text

Welcome to the Future

Slide 134

Slide 134 text

Selenium + Capybara on Rails

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

Testing Rails 5 apps

Slide 137

Slide 137 text

Test Runner

Slide 138

Slide 138 text

End to end testing

Slide 139

Slide 139 text

Much more than this

Slide 140

Slide 140 text

Testing guide guides.rubyonrails .org/testing.html

Slide 141

Slide 141 text

Rails 5 Blog Series blog.bigbinary.com /categories/Rails-5

Slide 142

Slide 142 text

This Week in Rails rails-weekly. ongoodbits.com

Slide 143

Slide 143 text

gracias! @_cha1tanya [email protected]