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

Secrets of testing Rails 5 apps

190

Secrets of testing Rails 5 apps

RailsConf 2016 talk

Prathamesh Sonpatki

May 05, 2016
Tweet

Transcript

  1. $ 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
  2. $ 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)
  3. $ 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
  4. $ 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
  5. $ 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
  6. $ 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
  7. $ 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
  8. $ 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
  9. $ 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 ✅
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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. 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. 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
  18. 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. BUT

  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. class Post < ActiveRecord::Base self.table_name = 'records' has_many :comments end

    class Comment < ActiveRecord::Base belongs_to :post end
  26. class ProductsControllerTest < ActionController::TestCase def test_create post :create { product:

    { name: "FIFA" } }, nil, { message: "Yay!" } assert_response :success end end Rails 4.x
  27. class ProductsControllerTest < ActionController::TestCase def test_create post :create nil, nil,

    { message: "Yay!" } assert_response :success end end Rails 4.x
  28. class ProductsControllerTest < ActionDispatch::IntegrationTest def test_create post product_url, params: {

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