Secrets of testing Rails 5 apps Red Dot Ruby Conf edition

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
June 23, 2016
120

Secrets of testing Rails 5 apps Red Dot Ruby Conf edition

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

June 23, 2016
Tweet

Transcript

  1. 1.
  2. 3.
  3. 6.
  4. 7.

  5. 8.
  6. 10.
  7. 11.
  8. 12.
  9. 13.
  10. 25.

    Rerun snippets ❌ Run tests by line ❌ Fail fast

    ❌ Coloured output ❌ Rails 4
  11. 27.
  12. 28.
  13. 33.

    $ 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
  14. 34.

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

    $ 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
  16. 46.

    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
  17. 47.

    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
  18. 48.

    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
  19. 49.

    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
  20. 53.

    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
  21. 55.

    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
  22. 58.
  23. 61.

    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
  24. 68.
  25. 69.

    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
  26. 70.

    class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do john = users(:john) post

    login_url, params: { email:john.email, password: john.password} end Rails 5
  27. 71.

    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
  28. 73.
  29. 75.
  30. 78.

    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
  31. 79.

    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
  32. 81.

    test "repos" do get :index assert_response :success, 'successfully renders index'

    assert_select ".repo-item-title", text: 'issue_triage_sandbox' end Rails 5
  33. 82.

    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
  34. 89.

    BUT

  35. 95.

    An optional hash of request parameters An optional hash of

    session variables An optional hash of flash values Rails 4
  36. 96.

    An optional hash of request parameters An optional hash of

    session variables An optional hash of flash values Rails 4
  37. 97.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create, { product:

    { name: "FIFA" } }, { user_id: User.first.id } assert_response :success end end Rails 4
  38. 98.

    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
  39. 99.

    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
  40. 100.

    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
  41. 101.
  42. 102.
  43. 105.

    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
  44. 106.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create, params: {

    product: { name: "FIFA" } } assert_response :success end end Rails 5
  45. 111.

    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
  46. 114.

    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
  47. 115.

    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
  48. 116.

    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
  49. 118.

    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
  50. 119.

    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
  51. 120.

    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
  52. 121.

    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
  53. 122.

    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