简体   繁体   English

使用 100% 覆盖率的 rspec 为模型编写测试用例

[英]Write test cases for model using rspec with 100% coverage

Hi I am working on RoR project with ruby-2.5.0 and Rails 5. I have a forgot_password model and i am writting test cases for it using rspec.嗨,我正在使用 ruby​​-2.5.0 和 Rails 5 开发 RoR 项目。我有一个 forgot_password 模型,我正在使用 rspec 为其编写测试用例。 I have two methods in model as follows:-我在模型中有两种方法如下:-

class ForgotPassword < ApplicationRecord
  before_create :create_token

  def self.create_record(user)
    forgot_password = create!(expiry: Time.zone.now + ENV['VALIDITY_PERIOD'].to_i.hours)
    send_forgot_password_email user, forgot_password
    forgot_password
  end

  def self.send_forgot_password_email(user, forgot_password)
    return if Rails.env == 'test'
    Mailjet::Send.create(from_email:  ENV['MIALJET_DEFAULT_FROM'],
                         from_name: ENV['MIALJET_FROM_NAME'],
                         to: user.email,
                         subject: 'Forgot Password',
                         text_part: forgot_password.token)
  end

  private

  def create_token
    self.token = SecureRandom.urlsafe_base64(nil, false)
  end
end

First method creates a record of forgot_password and another method send an email using mailjet.第一种方法创建forgot_password 的记录,另一种方法使用mailjet 发送电子邮件。 My spec is as follows:-我的规格如下:-

spec/models/forgot_password_spec.rb
require 'rails_helper'

RSpec.describe ForgotPassword, type: :model do
  user = FactoryBot.create(:user)

  describe '#create_record' do
    it 'do not raise_error' do
      expect { ForgotPassword.create_record(user) }.not_to raise_error
    end

    it 'increment the count of ForgotPassword' do
      expect { ForgotPassword.create_record(user) }.to change(ForgotPassword, :count)
        .from(0).to(1)
    end

    it 'return instance of ForgotPassword' do
      expect(ForgotPassword.create_record(user)).to be_instance_of(ForgotPassword)
    end

    it 'return nil when env is test' do
      expect(ForgotPassword.send_forgot_password_email(user,ForgotPassword.last)).to eq(nil)
    end
  end
end

When i run RAILS_ENV=test bundle exec rake i got Coverage (99.58%) is below the expected minimum coverage (100.00%) Please help me to write the missing case.当我运行RAILS_ENV=test bundle exec rake我得到的Coverage (99.58%) is below the expected minimum coverage (100.00%)请帮我写下缺失的案例。 when i remove return if Rails.env == 'test' this line from send_forgot_password_email method it covers 100%.当我从 send_forgot_password_email 方法中删除return if Rails.env == 'test'这一行时,它覆盖了 100%。 Please help me to fix it.请帮我修复它。 Thanks in advance.提前致谢。

Use MailJet through ActionMailer . 通过ActionMailer使用MailJet Make sure ActionMailer doesn't send out emails in the test env:确保ActionMailer不在测试环境中发送电子邮件:

# config/environments/test.rb
config.action_mailer.delivery_method = :test

and use enqueue_email RSpec matcher .并使用enqueue_email RSpec 匹配器

Also, don't do this:另外,不要这样做:

user = FactoryBot.create(:user)

outside of the example scope.在示例范围之外。 See this work-in-progress rubocop-rspec cop for explanation.请参阅此进行中的rubocop-rspec cop以获取解释。 If your intent is to reuse the record in several examples, take a look at let_it_be from test-prof .如果您打算在多个示例中重用记录,请查看test-prof let_it_be

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM