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

Secrets of Testing Rails 5 apps

110

Secrets of Testing Rails 5 apps

Talk given at Rails Pacific

Prathamesh Sonpatki

May 21, 2016
Tweet

Transcript

  1. $ 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
  2. $ 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
  3. $ 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
  4. $ 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
  5. $ 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
  6. $ 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
  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 Rails 5
  8. $ 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
  9. $ 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
  10. $ 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
  11. $ 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
  12. $ 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
  13. 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
  14. 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
  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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. test "repos" do get :index assert_response :success, 'successfully renders index'

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