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

Secrets of testing Rails 5 apps Red Dot Ruby Co...

Prathamesh Sonpatki
June 23, 2016
150

Secrets of testing Rails 5 apps Red Dot Ruby Conf edition

Prathamesh Sonpatki

June 23, 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 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. class ArticlesControllerTest < ActionDispatch::IntegrationTest setup do john = users(:john) post

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

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

  20. An optional hash of request parameters An optional hash of

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

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

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

    product: { name: "FIFA" } } assert_response :success end end Rails 5
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  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 assert_equal({ 'id' => Article.last.id, 'title' => 'Ahoy!' }, response.parsed_body) end end Rails 5