rspec - 为什么这个数组键比较失败?

时间:2012-10-10 01:41:43

标签: ruby-on-rails rspec matcher

Failure/Error: @group.attributes.keys.should include (Group.first.attributes.keys)     
   expected 

["id", "duration", "frequency", "period", "group_size", "location", "service", "area_of_need", "created_at", "updated_at", "therapist_id", "start_date", "end_date", "student_id", "adhoc"] 
to include  
["id", "duration", "frequency", "period", "group_size", "location", "service", "area_of_need", "created_at", "updated_at", "therapist_id", "start_date", "end_date", "student_id", "adhoc"]

测试:

@group.attributes.keys.should include (Group.first.attributes.keys) 

2 个答案:

答案 0 :(得分:2)

因为a.should includes(b)断言a.include? b为真且include?检查数组的元素是否包含对象,而不是一个数组是否等于另一个

[1].include? [1]
=> false

[1].include? 1
=> true

[[1]].include? [1]
=> true

答案 1 :(得分:1)

答案是,哈希比较在Ubuntu上失败但在Mac上运行。

我的解决方法是:

i=0
  while i < @group.attributes.count
  assert_equal @group.attributes[i], Group.first.attributes[i]
  i+= 1
end

# Comparing field by field as ruby hash comparison isn't working right - 
# but only on Ubuntu!