Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Prathamesh Sonpatki @_cha1tanya

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Pune, India

Slide 5

Slide 5 text

Rails issues team

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

I don’t have stickers

Slide 10

Slide 10 text

Stickers

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Do you have stickers?

Slide 15

Slide 15 text

T-Shirts are fine too

Slide 16

Slide 16 text

This talk is not about RSpec

Slide 17

Slide 17 text

It is about default Rails test suite

Slide 18

Slide 18 text

Secrets of Testing Rails 5 apps

Slide 19

Slide 19 text

Delete tests

Slide 20

Slide 20 text

Delete tests

Slide 21

Slide 21 text

Secrets of Testing Rails 5 apps

Slide 22

Slide 22 text

Running tests

Slide 23

Slide 23 text

rake test Rails 4

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Running tests without batteries

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Let’s make runing tests in Rails great again

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Test Runner

Slide 32

Slide 32 text

bin/rails test 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

$ 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 35

Slide 35 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 36

Slide 36 text

Powered by Minitest 5

Slide 37

Slide 37 text

Inspirations

Slide 38

Slide 38 text

RSpec mintiest-reporters maxitest m and others %

Slide 39

Slide 39 text

Writing tests

Slide 40

Slide 40 text

Unit tests Functional tests Integration tests

Slide 41

Slide 41 text

Unit tests Functional tests Integration tests

Slide 42

Slide 42 text

Controller tests

Slide 43

Slide 43 text

What do we test in a controller test

Slide 44

Slide 44 text

request —> response

Slide 45

Slide 45 text

request —> response

Slide 46

Slide 46 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 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

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 49

Slide 49 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 50

Slide 50 text

request —> state

Slide 51

Slide 51 text

Controller tests are easy

Slide 52

Slide 52 text

Controller tests actually lie

Slide 53

Slide 53 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 54

Slide 54 text

Integration tests

Slide 55

Slide 55 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 56

Slide 56 text

Almost same as controller test

Slide 57

Slide 57 text

Close to real world

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

https://github.com/eileencodes/ integration_performance_test

Slide 60

Slide 60 text

Integration controller tests are default in Rails 5

Slide 61

Slide 61 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 62

Slide 62 text

Functional controller tests are soft deprecated

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Might be extracted in separate gem in 5.1

Slide 65

Slide 65 text

Implications

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 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 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

Black Box

Slide 76

Slide 76 text

What not to test in integration style controller tests?

Slide 77

Slide 77 text

Don’t test controller internals

Slide 78

Slide 78 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 79

Slide 79 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 80

Slide 80 text

Scratching the surface

Slide 81

Slide 81 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 82

Slide 82 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 83

Slide 83 text

gem ‘rails-dom-testing’

Slide 84

Slide 84 text

Not just testing controller action in isolation

Slide 85

Slide 85 text

End to end testing

Slide 86

Slide 86 text

Controller View interface

Slide 87

Slide 87 text

Just an implementation detail

Slide 88

Slide 88 text

Deprecated assert_template assigns

Slide 89

Slide 89 text

BUT

Slide 90

Slide 90 text

Testing controller isolated from views

Slide 91

Slide 91 text

gem ‘rails-controller-testing’ % @tgx_world

Slide 92

Slide 92 text

HTTP Request methods

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Positional arguments Rails 4

Slide 95

Slide 95 text

An optional hash of request parameters An optional hash of session variables An optional hash of flash values Rails 4

Slide 96

Slide 96 text

An optional hash of request parameters An optional hash of session variables An optional hash of flash values Rails 4

Slide 97

Slide 97 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 98

Slide 98 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 99

Slide 99 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 100

Slide 100 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 101

Slide 101 text

Rails 4

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Ruby 2.2.2+ Rails 5

Slide 104

Slide 104 text

Keyword arguments Rails 5

Slide 105

Slide 105 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 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

Implications

Slide 110

Slide 110 text

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

Slide 111

Slide 111 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 112

Slide 112 text

Testing API requests

Slide 113

Slide 113 text

JSON or XML requests

Slide 114

Slide 114 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 115

Slide 115 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 116

Slide 116 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 117

Slide 117 text

Request Encoding

Slide 118

Slide 118 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 119

Slide 119 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 120

Slide 120 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 121

Slide 121 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 122

Slide 122 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 123

Slide 123 text

Other changes

Slide 124

Slide 124 text

Active Job Async adapter

Slide 125

Slide 125 text

Active Job Async adapter

Slide 126

Slide 126 text

Active Job Async adapter

Slide 127

Slide 127 text

Random test ordering

Slide 128

Slide 128 text

Testing Rails 5 apps

Slide 129

Slide 129 text

Test Runner

Slide 130

Slide 130 text

End to end testing

Slide 131

Slide 131 text

Much more than this

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

This Week in Rails rails-weekly. ongoodbits.com

Slide 134

Slide 134 text

Bye bye lah! @_cha1tanya [email protected]