Slide 1

Slide 1 text

Secrets of Testing Rails 5 apps

Slide 2

Slide 2 text

Prathamesh Sonpatki @_cha1tanya

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Stickers

Slide 5

Slide 5 text

Stickers

Slide 6

Slide 6 text

Do you have stickers?

Slide 7

Slide 7 text

CodeTriage.com

Slide 8

Slide 8 text

Rails 5 Yay!

Slide 9

Slide 9 text

So many changes

Slide 10

Slide 10 text

the way we test code

Slide 11

Slide 11 text

the way we run tests

Slide 12

Slide 12 text

the way we write tests

Slide 13

Slide 13 text

Testing Rails 5 apps

Slide 14

Slide 14 text

Running tests

Slide 15

Slide 15 text

Recap

Slide 16

Slide 16 text

rake test Rails 4.x

Slide 17

Slide 17 text

rake test:controllers Rails 4.x

Slide 18

Slide 18 text

Limitations

Slide 19

Slide 19 text

$ be rake test test/controllers/users_controller_test.rb Run options: --seed 8322 # Running: F Finished in 0.621790s, 1.6083 runs/s, 1.6083 assertions/s. 1) Failure: UsersControllerTest#test_contact_info_successfully_renders_page [railsconf_talk/test/controllers/users_controller_test.rb:9]: # didn't change by 1. Expected: 20 Actual: 19 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Slide 20

Slide 20 text

$ be rake test test/controllers/users_controller_test.rb:9 rake aborted! Don't know how to build task 'test/controllers/users_controller_test.rb:9' (See full trace by running task with --trace)

Slide 21

Slide 21 text

Test Runner

Slide 22

Slide 22 text

bin/rails test Rails 5

Slide 23

Slide 23 text

$ bin/rails test Run options: --seed 34502 # Running: ....... Finished in 2.437723s, 2.8715 runs/s, 4.9226 assertions/s. 7 runs, 12 assertions, 0 failures, 0 errors, 0 skips Rails 5

Slide 24

Slide 24 text

Rerun snippets

Slide 25

Slide 25 text

$ bin/rails test Run options: --seed 44837 # Running: .....F Failure: PostsControllerTest#test_should_create_post [railsconf_talk/test/controllers/ posts_controller_test.rb:19]: "Post.count" didn't change by 2. Expected: 4 Actual: 3 bin/rails test test/controllers/posts_controller_test.rb:18 . Finished in 0.622410s, 11.2466 runs/s, 16.0666 assertions/s. 7 runs, 10 assertions, 1 failures, 0 errors, 0 skips

Slide 26

Slide 26 text

$ bin/rails test Run options: --seed 44837 # Running: .....F Failure: PostsControllerTest#test_should_create_post [railsconf_talk/test/controllers/ posts_controller_test.rb:19]: "Post.count" didn't change by 2. Expected: 4 Actual: 3 bin/rails test test/controllers/posts_controller_test.rb:18 . Finished in 0.622410s, 11.2466 runs/s, 16.0666 assertions/s. 7 runs, 10 assertions, 1 failures, 0 errors, 0 skips

Slide 27

Slide 27 text

$ bin/rails test test/controllers/posts_controller_test.rb:18 Run options: --seed 5361 # Running: F Failure: PostsControllerTest#test_should_create_post [railsconf_talk/test/controllers/ posts_controller_test.rb:19]: "Post.count" didn't change by 2. Expected: 4 Actual: 3 bin/rails test test/controllers/posts_controller_test.rb:18 Finished in 0.456947s, 2.1884 runs/s, 2.1884 assertions/s. 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Slide 28

Slide 28 text

Documentation

Slide 29

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

Slide 30

Slide 30 text

Running multiple tests

Slide 31

Slide 31 text

$ bin/rails test test/models/user_test.rb:27 test/models/post_test.rb:42

Slide 32

Slide 32 text

$ bin/rails test test/controllers test/integration

Slide 33

Slide 33 text

$ bin/rails test test/models/user_test.rb:27:89

Slide 34

Slide 34 text

Backtrace

Slide 35

Slide 35 text

$ BACKTRACE=1 bin/rake test test/models/post_test.rb Rails 4.x

Slide 36

Slide 36 text

$ bin/rails test test/models/post_test.rb -b Rails 5

Slide 37

Slide 37 text

Fail fast

Slide 38

Slide 38 text

$ bin/rails test -f Rails 5

Slide 39

Slide 39 text

$ bin/rails t -f Run options: -f --seed 4299 # Running: ...F Failure: PostsControllerTest#test_should_create_post [railsconf_talk/test/controllers/ posts_controller_test.rb:19]: "Post.count" didn't change by 2. Expected: 4 Actual: 3 bin/rails test test/controllers/posts_controller_test.rb:18 Interrupted. Exiting... Finished in 0.677870s, 5.9008 runs/s, 7.3760 assertions/s. 4 runs, 5 assertions, 1 failures, 0 errors, 0 skips

Slide 40

Slide 40 text

Colored output

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

$ bin/rails t Run options: --seed 16545 # Running: ...F Failure: PostsControllerTest#test_should_create_post [/Users/prathamesh/Projects/ reproductions/railsconf_talk/test/controllers/posts_controller_test.rb: 19]: "Post.count" didn't change by 2. Expected: 4 Actual: 3 ✅

Slide 43

Slide 43 text

Minitest 5

Slide 44

Slide 44 text

Rails plugin + Custom reporter

Slide 45

Slide 45 text

Inspirations

Slide 46

Slide 46 text

RSpec mintiest-reporters maxitest and Others

Slide 47

Slide 47 text

Controller tests

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

Slide 49

Slide 49 text

class ArticlesControllerTest < 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 Rails 5

Slide 50

Slide 50 text

class ArticlesControllerTest < 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 Rails 5

Slide 51

Slide 51 text

class ArticlesControllerTest < 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 Rails 5

Slide 52

Slide 52 text

class ArticlesControllerTest < 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 Rails 5

Slide 53

Slide 53 text

Why?

Slide 54

Slide 54 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.x

Slide 55

Slide 55 text

class ArticlesControllerTest < 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 Rails 5

Slide 56

Slide 56 text

Almost same!

Slide 57

Slide 57 text

Integration tests are slow Rails 4

Slide 58

Slide 58 text

Integration tests were slow Rails 5

Slide 59

Slide 59 text

@eileencodes

Slide 60

Slide 60 text

Close to real world

Slide 61

Slide 61 text

Rails 5 generates integration controller tests

Slide 62

Slide 62 text

Old apps?

Slide 63

Slide 63 text

Backward compatible

Slide 64

Slide 64 text

ActionController::TestCase gem Rails 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.x

Slide 67

Slide 67 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do post login_url, params: { .. } end end Rails 5

Slide 68

Slide 68 text

class ArticlesControllerTest < ActionController::TestCase test ‘index’ do @request.headers["Accept"] = "text/plain, text/html” get :index end end Rails 4.x

Slide 69

Slide 69 text

class ArticlesControllerTest < ActionDispatch::IntegrationTest test ‘index’ do get articles_url, params: { .. }, headers: {} end end Rails 5

Slide 70

Slide 70 text

What do we actually test?

Slide 71

Slide 71 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.x

Slide 72

Slide 72 text

:action Rails 4.x

Slide 73

Slide 73 text

instance variables Rails 4.x

Slide 74

Slide 74 text

status code Rails 4.x

Slide 75

Slide 75 text

template Rails 4.x

Slide 76

Slide 76 text

HTML Rails 4.x

Slide 77

Slide 77 text

class ArticlesControllerTest < 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 Rails 5

Slide 78

Slide 78 text

request Rails 5

Slide 79

Slide 79 text

status code Rails 5

Slide 80

Slide 80 text

HTML Rails 5

Slide 81

Slide 81 text

But.. Rails 5

Slide 82

Slide 82 text

can’t test instance variables Rails 5

Slide 83

Slide 83 text

can’t test which template rendered Rails 5

Slide 84

Slide 84 text

assert_template Deprecated assigns Rails 5

Slide 85

Slide 85 text

Controller internals Not recommended Rails 5

Slide 86

Slide 86 text

test actual HTML Recommended Rails 5

Slide 87

Slide 87 text

assert_select Recommended Rails 5

Slide 88

Slide 88 text

gem ‘rails-dom-testing’ Recommended Rails 5

Slide 89

Slide 89 text

Not just testing controller action Rails 5

Slide 90

Slide 90 text

Controller + View Rails 5

Slide 91

Slide 91 text

There is a space between controller and views

Slide 92

Slide 92 text

Don’t go there! Rails 5

Slide 93

Slide 93 text

Controller View interface Rails 5

Slide 94

Slide 94 text

Just an implementation detail Rails 5

Slide 95

Slide 95 text

BUT

Slide 96

Slide 96 text

Why do we test controller internals

Slide 97

Slide 97 text

Correct template gets rendered

Slide 98

Slide 98 text

Verify correct data gets passed to views

Slide 99

Slide 99 text

Instance variables

Slide 100

Slide 100 text

THE way to send data to views

Slide 101

Slide 101 text

The unwritten contract

Slide 102

Slide 102 text

Interface testing

Slide 103

Slide 103 text

Testing controllers the old way

Slide 104

Slide 104 text

Isolated from views

Slide 105

Slide 105 text

No longer recommended

Slide 106

Slide 106 text

But sometimes needed

Slide 107

Slide 107 text

Controller via engine

Slide 108

Slide 108 text

gem ‘rails-controller-testing’

Slide 109

Slide 109 text

Testing API apps

Slide 110

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

Slide 111 text

JSON or XML requests

Slide 112

Slide 112 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.x

Slide 113

Slide 113 text

Request Encoding

Slide 114

Slide 114 text

`as` encoder

Slide 115

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

Slide 116 text

ActionDispatch::IntegrationTest.register_encoder

Slide 117

Slide 117 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.x

Slide 118

Slide 118 text

Response parsing

Slide 119

Slide 119 text

response.parsed_body

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!' }, response.parsed_body) end end Rails 5

Slide 121

Slide 121 text

The other changes

Slide 122

Slide 122 text

Active Job Async adapter

Slide 123

Slide 123 text

Random test ordering

Slide 124

Slide 124 text

Fixtures

Slide 125

Slide 125 text

file_fixture

Slide 126

Slide 126 text

test/fixtures/files

Slide 127

Slide 127 text

file_fixture(“response.json”) #=> Pathname

Slide 128

Slide 128 text

file_fixture(“response.json”).read

Slide 129

Slide 129 text

file_fixture(“response.json”).size

Slide 130

Slide 130 text

ActiveSupport::TestCase.file_fixture_path

Slide 131

Slide 131 text

Custom AR classes with fixtures

Slide 132

Slide 132 text

class Post < ActiveRecord::Base self.table_name = 'records' has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end

Slide 133

Slide 133 text

# records.yml # comments.yml

Slide 134

Slide 134 text

# test_helper.rb class ActiveSupport::TestCase set_fixture_class records: Post end

Slide 135

Slide 135 text

$ bin/rails db:schema:load

Slide 136

Slide 136 text

Metadata can be added in Rails 5

Slide 137

Slide 137 text

# records.yml _fixture: model_class: Post blurrgh: name: Rails 5 final release

Slide 138

Slide 138 text

Request methods

Slide 139

Slide 139 text

get post patch delete

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

Positional arguments Rails 4.x

Slide 142

Slide 142 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create { product: { name: "FIFA" } }, nil, { message: "Yay!" } assert_response :success end end Rails 4.x

Slide 143

Slide 143 text

class ProductsControllerTest < ActionController::TestCase def test_create post :create nil, nil, { message: "Yay!" } assert_response :success end end Rails 4.x

Slide 144

Slide 144 text

Keyword arguments Rails 5

Slide 145

Slide 145 text

Ruby 2.2.2+

Slide 146

Slide 146 text

class ProductsControllerTest < ActionDispatch::IntegrationTest def test_create post product_url, params: { product: { name: "FIFA" } } assert_response :success end end Rails 5

Slide 147

Slide 147 text

Old tests

Slide 148

Slide 148 text

ActionDispatch::ControllerTest #=> params flash session xhr

Slide 149

Slide 149 text

New tests

Slide 150

Slide 150 text

ActionDispatch::IntegrationTest #=> params env headers xhr

Slide 151

Slide 151 text

Testing Rails 5 apps

Slide 152

Slide 152 text

Better experience

Slide 153

Slide 153 text

More focus on end to end testing

Slide 154

Slide 154 text

Controller + View

Slide 155

Slide 155 text

Happy testing!

Slide 156

Slide 156 text

May the fourth

Slide 157

Slide 157 text

May the fifth

Slide 158

Slide 158 text

With Rails default test stack

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

RSpec and Rails 5 Justin Searls

Slide 161

Slide 161 text

Want to know more about Rails 5?

Slide 162

Slide 162 text

Rails 5 Blog Series http://blog.bigbinary.com/categories/ Rails%205

Slide 163

Slide 163 text

https://rails-weekly.ongoodbits.com/

Slide 164

Slide 164 text

RailsConf edition coming sooooooooooon!

Slide 165

Slide 165 text


 Thank You @_cha1tanya