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

时间:2015-02-27 04:08:03

标签: ruby unit-testing hash rspec

我有一个班级:

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把钥匙。

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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

简单!