简体   繁体   English

如何在 Rspec-Rails 中引发运行时错误

[英]How to Raise Runtime Error in Rspec-Rails

I have to test a code where I am raising some errors, I tried several techniques but it failed.我必须测试出现一些错误的代码,我尝试了几种技术但都失败了。 The structure of the class is defined below: class的结构定义如下:

SchemaController:架构控制器:

class SchemasController < ApplicationController
  def index
    @get_schema = Api::AnalyticsQueryBuilderMetadataService::Schema.show
  end
end

Show method under Api -> AnalyticsQueryBuilderMetadataService -> Schema.rb file: Api -> AnalyticsQueryBuilderMetadataService -> Schema.rb文件下的显示方法:

def self.show
        params = { 'limit' => 40 }
        response = Api::Connection.initiate_request('entities', params)
        if response.nil?
           Rails.logger.error 'Data not found for ClientId '
           raise 'Data not found'
        else
           get_schema(response)
        end
end

Rspec test I wrote for schema_spec.rb:我为 schema_spec.rb 编写的 Rspec 测试:

require 'rails_helper'
require 'spec_helper'

RSpec.describe Api::AnalyticsQueryBuilderMetadataService::Schema do
  describe 'GET all schema' do
    before do
      # allow_any_instance_of(SchemasController).to receive(:connection).and_return({})
      #binding.pry
      allow(Api::Connection).to receive(:initiate_request).and_return(nil)
    end

    context 'When no json body is passed' do
      it 'Raises NoMethodError' do
        # obj = SchemasController.new
        result = Api::AnalyticsQueryBuilderMetadataService::Schema.show()
        # expect {result}.to raise_error(RuntimeError)
        expect{result}.to raise_error
      end

      
    end
  end
end

But It is giving error as:但它给出的错误是:

Failures:

  1) Api::AnalyticsQueryBuilderMetadataService::Schema GET all schema When no json body is passed Raises NoMethodError
     Failure/Error: raise 'Data not found'
     
     RuntimeError:
       Data not found
     # ./app/lib/api/analytics_query_builder_metadata_service/schema.rb:22:in `show'
     # ./spec/lib/api/analytics_query_builder_metadata_service/schema_spec.rb:17:in `block (4 levels) in <top (required)>'

Finished in 2.3 seconds (files took 5.63 seconds to load)
44 examples, 1 failure

Failed examples:

rspec ./spec/lib/api/analytics_query_builder_metadata_service/schema_spec.rb:15 # Api::AnalyticsQueryBuilderMetadataService::Schema GET all schema When no json body is passed Raises NoMethodError

Help me to solve this.帮我解决这个问题。

From the docs;来自文档;

Use the raise_error matcher to specify that a block of code raises an error.使用 raise_error 匹配器指定代码块引发错误。

It means that the code in the block should be the one raising the error, but in your case the error is being raised when you declare the result variable.这意味着块中的代码应该是引发错误的代码,但在您的情况下,当您声明result变量时会引发错误。

To make it work you can skip the variable declaration and pass the variable value as the expect block;要使其工作,您可以跳过变量声明并将变量值作为expect块传递;

expect { Api::AnalyticsQueryBuilderMetadataService::Schema.show }
  .to raise_error(StandardError, 'Data not found')

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

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