How to write test cases for ruby on rails module without an associated database table?

Viewed 48

I have below module

Organization::EmployeeApis

I have the get_employee method in this module.

def get_employee(parameters)
  request = {
    employee_data: {
      name: {
        legal_name: {
          name_details: {
            first_name: parameters["first_name"],
            middle_name: parameters["middle_name"],
            surname: parameters["surname"]
          }
        }
      },
      contact_data: {
        email: parameters["email"]
      }
    }
  }
  employee = Employee.new(:get_employee, :get_employee_request, parameters["organization_id"])
  employee.get_response(employee)
end

I need to write test cases for the above module which is without an associated database table?

1 Answers

You can create a new class, include your module, and test it:

let(:dummy) do
  Class.new do
    include TheModule
  end
end

subject { dummy.new.get_employee(parameters) }

context "..." do
  let(:parameters) { ... }

  it "..." do
    expect(subject).to eq(...)
  end
end

It doesn't imply there should be a database table anywhere, just in the case Employee is a model backed by a database table, you'll need it. For the module you don't need one, it just depends on what it does.

Related