【问题标题】:How to create mock test cases for scalar valued function如何为标量值函数创建模拟测试用例
【发布时间】:2017-04-11 12:47:09
【问题描述】:

我正在调用一个标量值函数,但很难弄清楚如何为它创建模拟测试用例。

我只想创建一些模拟测试用例,例如,

  1. 函数成功返回一个值
  2. 函数返回转换错误

我在下面尝试了一些方法,但在这里我必须调用实际的函数,我不想这样做,因为如果数据库更改会导致测试用例失败,因此想要模拟用例。

require 'rails_helper'

describe ScalarSqlFunction::Base do
  describe '#exec' do
    let!(:success) { 0 }

    context 'with valid input' do
      let(:response) {
        ScalarSqlFunction::CalcTotal(105)
      }
      before do
        allow(response)
          .to receive(return_code).and_return(success)
      end
      it 'returns success' do
        expect(response.return_code).to eq(success)
      end
    end
  end
end

【问题讨论】:

    标签: ruby ruby-on-rails-4 rspec rspec-rails


    【解决方案1】:

    您可以为 ScalarSqlFunction::CalcTotal 创建一个模拟对象

    require 'rails_helper'
    
    describe ScalarSqlFunction::Base do
    describe '#exec' do
      let!(:success) { 0 }
    
    context 'with valid input' do
      let(:response) { double('Response') }
      before do
        allow(response)
          .to receive(return_code).and_return(success)
      end
      it 'returns success' do
        expect(response.return_code).to eq(success)
      end
    end
    end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 2021-01-03
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多