如何使用 rspec 在 API 路由中测试方法?

How can I test a method in an API route using rspec?

我是 ruby 的新手,我目前正在尝试为 gitlab 贡献一个新的 functionality.I 坚持编写一些测试用例。

pages_domains.rb

# frozen_string_literal: true

module API
  class PagesDomains < ::API::Base
    include PaginationParams

    feature_category :pages

    PAGES_DOMAINS_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(domain: API::NO_SLASH_URL_PART_REGEX)
 
    helpers do
      ##### my custom method   #####
      def is_page_domain_verified
        value=false
        result = VerifyPagesDomainService.new(pages_domain).execute
        if(result[:status] == :success)
          value=true
        end  
        value  
      end
      ##### my custom method   #####        
    end
 

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      before do
        require_pages_enabled!
      end

      # other api endpoint logic

      ########## my custom api endpoint logic ############
      desc 'Verify a pages domain' do
        success Entities::PagesDomain
      end
      params do
        requires :domain, type: String, desc: 'The domain'
      end
      get ":id/pages/domains/:domain/verify",requirements: PAGES_DOMAINS_ENDPOINT_REQUIREMENTS do
        authorize! :update_pages, user_project
        if pages_domain.persisted?

          if (is_page_domain_verified==false)
            render_api_error!("Failed to verify domain ownership", :unprocessable_entity)
          end

          present pages_domain, with: Entities::PagesDomain
        else
          render_validation_error!(pages_domain)
        end

      end
      ########## my custom api endpoint logic ############

      # other api enpoint logic
    end
  end
end

pages_domains_spec.rb 中,我添加了失败案例的测试用例,如下面提到的代码

pages_domains_spec.rb

  describe 'GET verify for page domain', :focus do
    
    context 'Domain Verifications' do
      it 'returns unprocessable_entity if failed to verify' do
        get api(route_domain_verify, admin)
        expect(response).to have_gitlab_http_status(:unprocessable_entity)
      end
    end  
  end

现在如何编写成功的测试用例? 为此,我需要从方法 is_page_domain_verified 中 return true。我探索了存根,但不幸地理解了它。 我尝试了以下代码来测试使用存根引用项目中的其他人的代码

pages_domains_spec.rb

  describe 'GET verify for page domain', :focus do
    
    context 'Domain Verifications' do
      before do
        allow(PagesDomains.is_page_domain_verified).to receive(:get).and_return(true)
      end

      it 'returns ok if verification successful' do
        get api(route_domain_verify, admin)
        expect(response).to have_gitlab_http_status(:ok)
      end
    end  
  end

当我 运行 时,我得到 NoMethodError。请帮助编写成功的测试用例。

Failures:

  1) API::PagesDomains GET verify for page domain Domain Verifications returns ok if verification successful
     Failure/Error: allow(PagesDomains.is_page_domain_verified).to receive(:get).and_return(true)
     
     NoMethodError:
       undefined method `is_page_domain_verified' for PagesDomains:Module
     # ./spec/requests/api/pages_domains_spec.rb:60:in `block (4 levels) in <top (required)>'
     # ./spec/spec_helper.rb:390:in `block (3 levels) in <top (required)>'
     # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:381:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:377:in `block (3 levels) in <top (required)>'
     # ./lib/gitlab/application_context.rb:31:in `with_raw_context'
     # ./spec/spec_helper.rb:377:in `block (2 levels) in <top (required)>'

Finished in 6.18 seconds (files took 36.48 seconds to load)
1 example, 1 failure

谢谢。

您看到的错误来自 PagesDomains.is_page_domain_verified 行,is_page_domain_verified 未在 PagesDomains class 上定义,因为它是辅助方法。

我们可以访问控制器助手来存根这个方法,但我不确定我喜欢这种方法。一种选择是 stub the instancesVerifyPagesDomainService class.

类似于:

allow_any_instance_of(VerifyPagesDomainService).to receive(:execute) and_return({status: :failed})