Secrets of Testing Rails 5 apps

1b0973b64704738dbc8ce24d8382bb1f?s=47 Prathamesh
May 21, 2016
91

Secrets of Testing Rails 5 apps

Talk given at Rails Pacific

1b0973b64704738dbc8ce24d8382bb1f?s=128

Prathamesh

May 21, 2016
Tweet

Transcript

  1. 1.
  2. 3.
  3. 9.
  4. 10.
  5. 13.
  6. 19.
  7. 29.

    $ be rake test test/controllers/users_controller_test.rb Run options: --seed 8322 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 Rails 4
  8. 30.

    $ be rake test test/controllers/users_controller_test.rb Run options: --seed 8322 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 Rails 4
  9. 32.

    $ 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) Rails 4
  10. 36.
  11. 40.
  12. 42.

    $ bin/rake -T test rake test # Runs all tests

    in test folder rake test:all # Run tests quickly by merging all types and not resetting db rake test:all:db # Run tests quickly, but also reset db rake test:db # Run tests quickly, but also reset db Rails 4
  13. 43.

    $ rails Usage: rails COMMAND [ARGS] The most common rails

    commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") new Create a new Rails application. "rails new my_app" creates a new application called MyApp in "./my_app" In addition to those, there are: destroy Undo code generated with "generate" (short-cut alias: "d") plugin new Generates skeleton for developing a Rails plugin runner Run a piece of code in the application environment (short-cut alias: "r") All commands can be run with -h (or --help) for more information. Rails 4
  14. 45.
  15. 46.
  16. 51.

    $ 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
  17. 53.

    $ 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
  18. 54.

    $ 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
  19. 55.

    $ 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
  20. 58.

    $ 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 Rails 5
  21. 59.

    $ 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 Rails 5
  22. 64.
  23. 65.
  24. 68.

    $ 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 ✨ Rails 5
  25. 73.
  26. 82.

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

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

    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
  29. 86.
  30. 88.

    class ArticlesControllerTest < ActionController::TestCase test "should create article" do assert_difference('Article.count')

    do assert assigns(:article) post :create, article: { title: @article.title } end assert_redirected_to article_path(assigns(:article)) end end Rails 4
  31. 90.

    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
  32. 93.
  33. 94.
  34. 96.

    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 test "should get new" do get new_user_url assert_response :success end test "should create user" do assert_difference('User.count') do post users_url, params: { user: { name: @user.name } } end assert_redirected_to user_path(User.last) end end Rails 5
  35. 108.

    class ArticlesControllerTest < ActionDispatch::IntegrationTest test ‘index’ do @request.headers["Accept"] = "text/plain,

    text/html” get :index end end # NoMethodError: undefined method `headers' for nil:NilClass Rails 5
  36. 113.

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

    assert_not_nil assigns(:repos), 'assigns to repos' assert_not_nil assigns(:repos_subs), 'assigns to repos_subs' assert_template :index, 'render index template' end Rails 4
  37. 114.

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

    assert_not_nil assigns(:repos), 'assigns to repos' assert_not_nil assigns(:repos_subs), 'assigns to repos_subs' assert_template :index, 'render index template' end Rails 4
  38. 115.

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

    assert_not_nil assigns(:repos), 'assigns to repos' assert_not_nil assigns(:repos_subs), 'assigns to repos_subs' assert_template :index, 'render index template' end Rails 4
  39. 118.

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

    assert_select ".repo-item-title", text: 'issue_triage_sandbox' end Rails 5
  40. 119.

    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
  41. 127.

    BUT