繁体   English   中英

在Ruby on Rails应用程序中应使用Rspec测试什么测试

[英]What a test should test with Rspec in Ruby on Rails application

我是Rspec的初学者,实际上,我们要求我对已构建但从未进行过测试的某些方法进行Rspec测试(他们在构建该方法之前不编写测试)。

现在,我正努力了解如何测试我的方法,下面是示例:

 class ConnectorJob < ActiveJob::Base

  queue_as :connector

  def perform(tenant_id = nil, debug = false)
    tenant_to_sync = Tenant.to_sync(tenant_id)
    return if tenant_to_sync.empty?
    tenant_to_sync.each do |tenant|
      service = MyAPP::ContactToSync.new(tenant, debug).call
      if service.success?
        ConnectorService::Synchronization.new(
          tenant, service.data, debug
        ).call
      end
    end
  end
end

我应该对此进行哪些测试? 我应该测试返回值是否正确,或者是否调用了其他方法?

这是我试图做的

    require "rails_helper"

RSpec.describe ConnectorJob, type: :job do
  it 'is in connector queue' do
    expect(ConnectorJob.new.queue_name).to eq('connector')
  end

  describe 'perform' do
    let (:tenant) { create(:tenant) }
    let (:job) { ConnectorJob.new.perform(tenant.id) }

  context 'with empty tenant' do
      it { expect(ConnectorJob.new.perform(@tenant.id)).to eq nil }
    end

    context 'with tenant' do
      it { expect(ConnectorJob.new.perform(tenant.id)).to eq job }
    end
  end
end

如您所见,我的上一个测试没有意义,但是我不知道我应该在Rspec上写些什么来预期这种方法的结果。

如果我检查Rspec的覆盖范围,则Rspec告诉我我覆盖了我方法的100%,但是我不确定那是正确的。

我希望我很清楚,随时问我更多细节。

谢谢你们

我认为您应该测试最终结果,我的意思是在调用ConnectorService::Synchronization.new(...).call并测试三种情况,例如,如果此调用创建了新用户,则应该对其进行测试:

如果tenant_to_sync.empty? == true tenant_to_sync.empty? == true

 context 'with empty tenant' do
   it { expect(ConnectorJob.new.perform(@tenant.id)).to change(User.count).by(0) }
 end

如果service.success? == false service.success? == false

context 'MyAPP::ContactToSync return false' do
   it { expect(ConnectorJob.new.perform(@tenant.id)).to change(User.count).by(0) }
 end

如果service.success? == true service.success? == true

context 'success' do
   it { expect(ConnectorJob.new.perform(@tenant.id)).to change(User.count).by(1) }
 end

它应该足以涵盖所有情况。

暂无
暂无

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

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