how to start minitest - Ruby on Rails

From my previous post all the setup’s done for Rails, for minitest and Capybara.

http://www.rubydoc.info/github/jnicklas/capybara
https://github.com/blowmage/minitest-rails
https://github.com/blowmage/minitest-rails-capybara

I read those documentations and start testing, by generating test files, with following rails generate commands:

$ rails generate
Rails:
  #other generators omitted
  integration_test

Minitest:
  minitest:feature
  minitest:install

TestUnit:
  test_unit:controller
  test_unit:integration
  test_unit:model
  test_unit:scaffold

I use these generators, I generate these test classes in which I put test methods. If I memorize all these class names, I will be able to do testing without using generators.

  minitest:feature
    Capybara::Rails::TestCase (test/features directory)

  minitest:install
    ActiveSupport::TestCase (test/test_helper.rb)

  test_unit:controller
  test_unit:scaffold
    ActionController::TestCase (test/controllers)

  integration_test
  test_unit:integration
    ActionDispatch::IntegrationTest (test/integration)

  test_unit:model
    ActiveSupport::TestCase (test/models)

By the way, after following test generator setup, I have its test files by generating a model or controller.

# config/application.rb
+
+    config.generators do |g|
+      g.test_framework :minitest, spec: false, fixture: true
+    end
   end

By this generator setup, I should be able to generate model test files by generating its model.

$ rails generate model Team
      invoke  active_record
      create    db/migrate/20150504195636_create_teams.rb
      create    app/models/team.rb
      invoke    minitest
      create      test/models/team_test.rb
      create      test/fixtures/teams.yml

generates,

# test/models/team_test.rb
require "test_helper"

class TeamTest < ActiveSupport::TestCase

  def team
    @team ||= Team.new
  end

  def test_valid
    assert team.valid?
  end

end

And for each test generators I tried what test files are look like. Model test looks like,

$ rails generate test_unit:model Team
      create  test/models/team_test.rb

# test/models/team_test.rb
require 'test_helper'

class TeamTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true
  # end
end

Feature test (Capybara test) looks like,

$ rails generate minitest:feature save_teams
      create  test/features/save_teams_test.rb

# test/features/save_teams_test.rb
require "test_helper"

class SaveTeamsTest < Capybara::Rails::TestCase
  test "sanity" do
    visit root_path
    assert_content page, "Hello World"
    refute_content page, "Goobye All!"
  end
end

Integration test (Rails integration test / Minitest integration test - two generators do basically the same thing) looks like,

$ rails generate integration_test login_users
      invoke  minitest
      create    test/integration/login_users_test.rb

# test/integration/login_users_test.rb
require "test_helper"

class LoginUsersTest < ActionDispatch::IntegrationTest
  def test_sanity
    flunk "Need real tests"
  end
end

$ rails generate test_unit:integration list_teams
      create  test/integration/list_teams_test.rb

# test/integration/list_teams_test.rb
require 'test_helper'

class ListTeamsTest < ActionDispatch::IntegrationTest
  # test "the truth" do
  #   assert true
  # end
end

Controller test looks like,

$ rails generate test_unit:controller teams           
      create  test/controllers/teams_controller_test.rb

# test/controllers/teams_controller_test.rb
require 'test_helper'

class TeamsControllerTest < ActionController::TestCase
  # test "the truth" do
  #   assert true
  # end
end

And interestingly, test_unit:scaffold generates a full-size controller test.

$ rails generate test_unit:scaffold user name email password:digest
      create  test/controllers/users_controller_test.rb

# test/controllers/users_controller_test.rb
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:users)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create user" do
    assert_difference('User.count') do
      post :create, user: { email: @user.email, name: @user.name, password: 'secret', password_confirmation: 'secret' }
    end

    assert_redirected_to user_path(assigns(:user))
  end

  test "should show user" do
    get :show, id: @user
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @user
    assert_response :success
  end

  test "should update user" do
    patch :update, id: @user, user: { email: @user.email, name: @user.name, password: 'secret', password_confirmation: 'secret' }
    assert_redirected_to user_path(assigns(:user))
  end

  test "should destroy user" do
    assert_difference('User.count', -1) do
      delete :destroy, id: @user
    end

    assert_redirected_to users_path
  end
end

And we got all the matchers on documentation above, again, it’s time to go testing!

 
10
Kudos
 
10
Kudos

Now read this

using has_secure_password

For authentication, we need to do following three: to model, has_secure_password to table, password_digest bundle bcrypt gem So, we add has_secure_password to User model, has_secure_password and it requires gem ‘bcrypt-ruby’ to the... Continue →