如何测试这种方法?(R规格)

拉帕帕
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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章