Back to Repositories

Testing Custom Registration Controller Block Yielding in Devise

This test suite validates the Custom::RegistrationsController in Devise by testing resource block yielding functionality. It ensures proper behavior of user registration and update workflows through comprehensive controller tests.

Test Coverage Overview

The test suite provides thorough coverage of registration controller actions including create, update, and new operations.

  • Tests successful and failed user creation scenarios
  • Validates update operations with authenticated users
  • Verifies proper resource block yielding in all controller actions
  • Covers authentication state handling

Implementation Analysis

The implementation uses Devise’s ControllerTestCase framework with controller helpers for authentication testing. The suite employs a setup block to establish test prerequisites including user creation and authentication mapping.

  • Utilizes Devise::Test::ControllerHelpers for authentication
  • Implements HTTP verb testing (POST, PUT, GET)
  • Uses block yield verification through custom controller methods

Technical Details

  • Test Framework: Minitest
  • Authentication: Devise
  • Helper Modules: Devise::Test::ControllerHelpers
  • Setup Requirements: User model, Custom::RegistrationsController
  • Test Environment: Controller test context

Best Practices Demonstrated

The test suite exemplifies several testing best practices for controller functionality verification.

  • Isolated test cases for success and failure scenarios
  • Proper test setup and environment configuration
  • Clear assertion messages
  • Consistent testing patterns across different controller actions
  • Effective use of helper modules and test frameworks

heartcombo/devise

test/controllers/custom_registrations_controller_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class CustomRegistrationsControllerTest < Devise::ControllerTestCase
  tests Custom::RegistrationsController

  include Devise::Test::ControllerHelpers

  setup do
    request.env["devise.mapping"] = Devise.mappings[:user]
    @password = 'password'
    @user = create_user(password: @password, password_confirmation: @password).tap(&:confirm)
  end

  test "yield resource to block on create success" do
    post :create, params: { user: { email: "[email protected]", password: "password", password_confirmation: "password" } }
    assert @controller.create_block_called?, "create failed to yield resource to provided block"
  end

  test "yield resource to block on create failure" do
    post :create, params: { user: { } }
    assert @controller.create_block_called?, "create failed to yield resource to provided block"
  end

  test "yield resource to block on update success" do
    sign_in @user
    put :update, params: { user: { current_password: @password } }
    assert @controller.update_block_called?, "update failed to yield resource to provided block"
  end

  test "yield resource to block on update failure" do
    sign_in @user
    put :update, params: { user: { } }
    assert @controller.update_block_called?, "update failed to yield resource to provided block"
  end

  test "yield resource to block on new" do
    get :new
    assert @controller.new_block_called?, "new failed to yield resource to provided block"
  end
end