Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails 5 testing presentation given at Conferencia Rails

Prathamesh Sonpatki
October 15, 2016
160

Rails 5 testing presentation given at Conferencia Rails

Prathamesh Sonpatki

October 15, 2016
Tweet

Transcript

  1. Rerun snippets ❌ Run tests by line ❌ Fail fast

    ❌ Coloured output ❌ Rails 4
  2. $ 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
  3. $ 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
  4. $ 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. # Functional controller test post :create, article: { title: @article.title

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

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

    } # Integration test post articles_url, params: {article: { title: @article.title }} Rails 4
  13. 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
  14. 1. Prefer ActionDispatch::IntegrationTest for new Rails 5 apps. 2. Leave

    functional controller tests alone for upgraded Rails 4 apps.
  15. 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
  16. class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do john = users(:john) post

    login_url, params: { email: john.email, password: john.password } end Rails 5
  17. 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
  18. 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
  19. 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
  20. test "repos" do get :index assert_response :success, 'successfully renders index’

    assert_select ".repo-item-title", text: 'issue_triage_sandbox' end Rails 5
  21. 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
  22. BUT

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

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

    hash of session variables 3. An optional hash of flash values
  25. class ProductsControllerTest < ActionController::TestCase def test_create post :create, { product:

    { name: "FIFA" } }, { user_id: User.first.id } assert_response :success end end Rails 4
  26. 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
  27. 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
  28. 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
  29. 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
  30. class ProductsControllerTest < ActionController::TestCase def test_create post :create, params: {

    product: { name: "FIFA" } } assert_response :success end end Rails 5
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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