如何测试这个方法? (RSpec)

How to test this method? (RSpec)

def is_doctor
    user = User.find_by(role: 'doctor')
    "Name: #{user.name} | Email: #{user.email}| isActive: #{user.is_active}"  
end

好像是这样,但是不知道怎么正确实现↓

context 'test' do
  #it { expect(user.is_doctor).to eq("Taras") }
end

我假设 doctor?User 模型上的实例方法 + 您正在使用 Faker 这可能对您有很大帮助。

# models/user.rb
class User < ApplicationRecord

  def doctor?
    return 'Not a doc' unless role == 'doctor'

    "Name: #{name} | Email: #{email}| isActive: #{is_active}"
  end
end

# specs/models/user_spec.rb
describe User, type: :model do
  context 'with instance method' do

    describe '#doctor?' do
      subject { user. doctor? }

      context 'with a doctor' do
        let(:user) { create(:user, role: 'doctor') }

        it 'includes name' do
          expect(subject).to include("Name: #{user.name}")
        end
        
        it 'includes email' do
          expect(subject).to include("Email: #{email}")
        end

        it 'includes is_active' do
          expect(subject).to include("isActive: #{is_active}")
        end
      end

      context 'without doctor' do
        let(:user) { create(:user, role: 'foo') }

        it 'has static response' do
          expect(subject).to eq('Not a doc')
        end
      end
    end
  end
end