Secrets of testing Rails 5 apps

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
May 05, 2016
160

Secrets of testing Rails 5 apps

RailsConf 2016 talk

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

May 05, 2016
Tweet

Transcript

  1. 3.
  2. 15.
  3. 19.

    $ 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]: #<Proc:0x007fd76ce55b50@/railsconf_talk/test/controllers/ users_controller_test.rb:9 (lambda)> didn't change by 1. Expected: 20 Actual: 19 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
  4. 20.

    $ 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)
  5. 23.

    $ 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
  6. 25.

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

    $ 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
  8. 27.

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

    $ 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
  10. 34.
  11. 37.
  12. 39.

    $ 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
  13. 41.
  14. 42.

    $ 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 ✅
  15. 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.x
  16. 49.

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

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

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

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

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

    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
  23. 62.
  24. 71.

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

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

    BUT

  27. 110.

    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
  28. 112.

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

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

    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
  31. 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!' }, response.parsed_body) end end Rails 5
  32. 124.
  33. 132.

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

    class Comment < ActiveRecord::Base belongs_to :post end
  34. 140.
  35. 142.

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

    { name: "FIFA" } }, nil, { message: "Yay!" } assert_response :success end end Rails 4.x
  36. 143.

    class ProductsControllerTest < ActionController::TestCase def test_create post :create nil, nil,

    { message: "Yay!" } assert_response :success end end Rails 4.x
  37. 146.

    class ProductsControllerTest < ActionDispatch::IntegrationTest def test_create post product_url, params: {

    product: { name: "FIFA" } } assert_response :success end end Rails 5
  38. 147.
  39. 149.
  40. 159.