Rails 5 testing presentation given at Conferencia Rails

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
October 15, 2016
140

Rails 5 testing presentation given at Conferencia Rails

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

October 15, 2016
Tweet

Transcript

  1. 4.
  2. 8.
  3. 9.

  4. 11.
  5. 23.

    Rerun snippets ❌ Run tests by line ❌ Fail fast

    ❌ Coloured output ❌ Rails 4
  6. 25.
  7. 26.
  8. 31.

    $ 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
  9. 32.

    $ 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
  10. 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
  11. 44.

    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
  12. 45.

    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
  13. 46.
  14. 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
  15. 52.

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

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

    # Functional controller test post :create, article: { title: @article.title

    } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4
  18. 56.

    # Functional controller test post :create, article: { title: @article.title

    } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4
  19. 57.

    # Functional controller test post :create, article: { title: @article.title

    } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4
  20. 59.
  21. 62.

    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
  22. 66.

    1. Prefer ActionDispatch::IntegrationTest for new Rails 5 apps. 2. Leave

    functional controller tests alone for upgraded Rails 4 apps.
  23. 70.
  24. 71.

    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
  25. 72.

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

    login_url, params: { email: john.email, password: john.password } end Rails 5
  26. 73.

    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
  27. 75.
  28. 77.
  29. 80.

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

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

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

    assert_select ".repo-item-title", text: 'issue_triage_sandbox' end Rails 5
  32. 84.

    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
  33. 90.

    BUT

  34. 96.

    1. An optional hash of request parameters 2. An optional

    hash of session variables 3. An optional hash of flash values
  35. 97.

    1. An optional hash of request parameters 2. An optional

    hash of session variables 3. An optional hash of flash values
  36. 98.

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

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

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

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

    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
  40. 102.
  41. 103.
  42. 106.

    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
  43. 107.

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

    product: { name: "FIFA" } } assert_response :success end end Rails 5
  44. 116.

    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
  45. 118.
  46. 119.

    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
  47. 120.
  48. 124.

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

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

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

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

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

    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
  54. 131.

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

    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
  56. 135.