RSpec:如何测试哈希数组中密钥的存在?

问题描述 投票:3回答:4

我有课:

class ApiParser
  def initialize
    ..
  end

  def api_data
    # returns an array of hashes like so:
    # [{ answer: "yes", name: "steve b" age: 22, hometown: "chicago", ... },
    # { answer:"unsure", name: "tom z", age: 44, hometown: "baltimore" , ... },
    # { answer: "no", name: "the brah", age: nil, hometown: "SF", ... }, 
    # { ... }, { ... }, ... ]
  end
end

该方法返回一个哈希数组。数组的长度为50个元素。

每个哈希具有完全相同的键。大约有20个键。

我不确定对这种方法进行单元测试的最佳方法是什么。如何检查该方法是否确实在返回具有每个键的散列的数组?一些哈希值可能为零,因此我认为我不会测试这些值。

ruby unit-testing hash rspec
4个回答
7
投票

这将帮助:

describe "your test description" do
  let(:hash_keys) { [:one, :two].sort } # and so on

  subject(:array) { some_method_to_fetch_your_array }

  specify do
    expect(array.count).to eq 50

    array.each do |hash|
      # if you want to ensure only required keys exist
      expect(hash.keys).to contain_exactly(*hash_keys)
      # OR if keys are sortable
      # expect(hash.keys.sort).to eq(hash_keys)

      # if you want to ensure that at least the required keys exist
      expect(hash).to include(*hash_keys)
    end
  end
end

这种方法的一个问题:如果测试失败,您将很难准确找出导致失败的数组索引。添加自定义错误消息将有所帮助。 Something如下所示:

array.each_with_index do |hash, i|
  expect(hash.keys).to contain_exactly(*hash_keys), "Failed at index #{i}"
end

0
投票

假设arr是哈希数组。让:

a = arr.map { |h| h.keys.sort }.uniq

然后,仅当且仅当所有散列具有相同的n键:

a.size == 1 && a.first.size == n

这很容易测试。

如果为您提供了数组keys中的必需键,则测试为:

a.size == 1 && a.first == keys.sort

0
投票

我对此略有不同。错误报告不会告诉您太多,但它们会让您知道:

describe 'User Management: `/api/users`', type: :request do
  let(:required_keys) { %i(id email created_at updated_at) }
  let(:optional_keys) {
    %i(first_name last_name gender birthday bio phone role                                                                                     
       profile_image_url notification_preferences custom_group_order                                                                            
       archived timezone)
  }
  let(:keys) { required_keys + optional_keys }

  shared_examples 'a user object' do
    it 'has values for required keys' do
      subject.slice(*required_keys).values.should all be
    end

    its(:keys) { should include(*keys) }
  end

  shared_examples 'a users collection' do
    it { should be_an(Array) }

    it 'has all defined keys' do
      subject.map(&:keys).should all include(*keys)
    end

    it 'has values for required keys' do
      subject.map_send(:slice, *required_keys).map(&:values).flatten.should all be
    end
  end
end

这些的危险是它们不需要用户集合为非空。如果返回一个空数组,则通过。

我将这些测试包含在适当的尺寸检查中:

describe 'GET to /api/users/visible' do
  let(:user) { Fabricate(:user) }

  subject { json[:users] }

  shared_examples 'a correct response' do
    it_should_behave_like 'a users collection'

    specify { controller.should respond_with :success }

    it { should have(members.size).items }

    it 'returns matching user ids' do
      ids(subject).should =~ ids(members)
    end
  end

  context 'with no groups' do
    let(:members) { [] }

    before { get '/api/users/visible', nil, auth_headers(user) }

    it_should_behave_like 'a correct response'
  end
end

jsonids方法只是:

def json
  JSON.parse(response.body, symbolize_names: true) if response.try(:body).try(:present?)
end

def ids(*from)
  Array.wrap(*from).map do |item|
    if item.respond_to?(:id)
      item.send(:id)
    elsif item.is_a?(Hash)
      item[:id] || item['id']
    end
  end
end

0
投票

这仅有助于一行

describe '#api_data' do
  subject { ApiParser.new.api_data }
  let(:expected_keys) { [:key1, :key2, :key3] }

  it { is_expected.to all(contain_exactly(expected_keys)) }
end

简单!

© www.soinside.com 2019 - 2024. All rights reserved.