例如在测试套件中使用 "class_eval" (Ruby) 修改 类 是否安全?

Is it safe to modify classes for example using "class_eval" (Ruby) in the tests suite?

换句话说:class 修改(在测试中)是否有机会影响生产代码?

(此代码示例使用 Rspec 在 Rails 应用程序中进行测试)

我的控制器示例

在此控制器中创建了 ExternalModel。然后调用它的“inscription”方法并将结果分配给一个变量。它将结果用于控制器方法上的其他操作。

class ExampleController < ApplicationController
  def callback_page
    external_model = ExternalModel.new(argument)
    result = external_model.inscription

    render_error_json && return unless result['error_desc'].eql? 'OK'
    TransactionModel.create(token: result['token'])
  end
end

我的规范示例

在规范中,我修改了 ExternalModel,因此它 returns 我在调用 .inscription 方法时想要的:

ExternalModel.class_eval {
      def inscription(_fake_arguments)
        {
          'error_desc' => 'OK',
          'token' => '1234'
        }
      end
    }

这是完整的规范:

RSpec.describe 'Example management', type: :request do
  context 'callback_page' do
    it 'creates a transaction' do
      ExternalModel.class_eval {
        def inscription(_fake_arguments)
          {
            'error_desc' => 'OK',
            'token' => '1234'
          }
        end
      }

      expect {
        post(callback_page_path)
      }.to change(TransactionModel.all, :count).by(1)

      expect(response).to render_template(:callback_page)
    end
  end
end

你在这里试图实现的正是存根的用途:它们是一种在单个示例范围内有效伪造行为的方法,然后在示例具有 运行.

在您的示例中,大致如下所示:

allow_any_instance_of(ExternalModel).
  to receive(:inscription).
     and_return({ 'error_desc' => 'OK', 'token' => '1234' })

可以在 rspec-mocks gem 的文档中找到更多详细信息:https://relishapp.com/rspec/rspec-mocks/v/3-9/docs