我正在尝试为从不同类调用另一个方法的方法编写RSpec测试用例

时间:2020-02-03 10:43:38

标签: ruby

# NodePackageAssetFile

require 'asset_ingester/asset_type/base_asset_type'
require 'asset_ingester/helpers/github_asset_type_helpers'

module AssetIngester
  module AssetType
    class NodePackage < BaseAssetType
      def self.asset_type
        'node_package'
      end

      def self.search_terms
        ["'registry' in:file filename:.npmrc "]
      end

      # Public: Retrieves all node package assets from Github
      #
      # Returns an array of github asset objects sorted by full name
      def self.assets
        Helpers::GithubAssetTypeHelpers.assets(asset_type, search_terms)
      end
    end
  end
end

# Rspec I tried for asset method

 describe '#assets' do
    let(:nodePackage) do
      [id: 131690,
        name: 'acm-care-management-js',
        full_name: 'AcuteCaseManagementUI/acm-care-management-js',
        owner_name: 'AcuteCaseManagementUI',
        owner_url: 'https://github.cerner.com/api/v3/users/AcuteCaseManagementUI',
        owner_avatar_url: 'https://avatars.github.cerner.com/u/4095?',
        url: 'https://github.cerner.com/api/v3/repos/AcuteCaseManagementUI/acm- 
 care-management-js',
        html_url: 'https://github.cerner.com/AcuteCaseManagementUI/acm-care-management-js',
        asset_type: 'node_package']
      end
    it 'returns the node package assets' do
      allow(AssetIngester::Helpers::GithubAssetTypeHelpers).to receive(:assets).with('node_package',["'registry' in:file filename:.npmrc "]).and_return(nodePackage)
      key = AssetIngester::AssetType::NodePackage.assets
      expect(key).to eq(nodePackage)
    end
  end

我尝试过以此方式测试资产方法。测试通过了,但是,我想知道这是否是正确的方法。如果这种方法是完全错误的,那么请提出正确的方法。我尝试使用RSpec-Stub测试NodePackage类的资产方法。

1 个答案:

答案 0 :(得分:1)

使用RSpec模拟的消息期望

通常,RSpec支持使用message expectations测试方法调用。邮件期望值可用于双打测试,但您可以在RSpec Mocks documentation中阅读有关全部功能的更多信息。

相关问题