首页 \ 问答 \ Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)

Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)

我经常想要比较数组,并确保它们以任何顺序包含相同的元素。 在RSpec中有简单的方法吗?




array.to_set.should == another_array.to_set




array.sort.should == another_array.sort


I often want to compare arrays and make sure that they contain the same elements, in any order. Is there a concise way to do this in RSpec?

Here are methods that aren't acceptable:


For example:

expect(array.to_set).to eq another_array.to_set


array.to_set.should == another_array.to_set

This fails when the arrays contain duplicate items.


For example:

expect(array.sort).to eq another_array.sort


array.sort.should == another_array.sort

This fails when the arrays elements don't implement #<=>

更新时间:2019-07-12 10:31


尝试array.should =~ another_array

我可以找到的最好的文档是代码本身, 在这里

Use match_array, which takes another array as an argument, or contain_exactly, which takes each element as a separate argument, and is sometimes useful for readability. (docs)


expect([1, 2, 3]).to match_array [3, 2, 1]


expect([1, 2, 3]).to contain_exactly 3, 2, 1



设置RSpec测试的顺序(Set order of RSpec tests)

以数字方式命名_spec.rb文件: 01_first_spec.rb 02_second_spec.rb ... 创建.rspec文件 # .rspec --order defined 现在,在运行rspec ,文件应按排序顺序执行。 Name the _spec.rb files numerically: 01_first_spec.rb 02_second_spec.rb ... Create a .rspec file # .rspec --order defined Now, ...

按顺序放置数组变量(Put array variables in order)

试试重新分配c值: $data['c'] = array_values($data['c']); 它会重新索引你的c数组,但索引将从0开始。 如果你真的想从0开始,请尝试: $data['c'] = array_combine(range(1, count($data['c'])), array_values($data['c'])) Just try with reassigning c value: $data['c'] = array_values($data['c']); It wi ...

我应该如何以我想要的方式将关联数组与$ _FILES数组合并?(How should I merge the associative array with $_FILES array in a way I wanted?)

最简单的方法是遍历$_FILES数组并从字段名称的最后一段中提取id ...然后使用该-1作为将文件添加到结果数组的基础。 像这样的东西应该让你在那里(未经测试): foreach($_FILES as $k => $file){ $index_to_update = trim(substr($k, strrpos($k, "_")+1))-1; $res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = is ...

基于数组顺序查询数据库/基于其他数组的值排序数组(Query DB based off array order / sort array based off another array's values)

用这个 SELECT * FROM table WHERE id IN (5,9,17,19,456) ORDER BY id ASC; 所以,你会得到与任何id相匹配的行并将其ascending排列。 I've managed to solve my problem. $unorderedArray = array(); $orderedArray = array(); while($a = mysqli_fetch_assoc($r)) { array_push($unordered ...

JS从数组中删除重复项而没有更高阶函数(JS delete duplicated items from array without higher order functions)

使用对象作为帮助程序。 如果助手中出现一个值,则它不是唯一的,可以忽略。 如果它不在帮助器中它是唯一的,则将其推入结果数组,并将其添加到辅助对象。 const array = [1, 2, 3, 3, 1]; const removeDuplicate = (arr) => { const helperMap = {}; const result = []; for (let i = 0; i < arr.length; i++) { const item = ...

如何在不替换数组内容的情况下在javascript中将数组内容添加(复制)到另一个数组?(How to add(copy) an array content to another array in javascript without replacing the content of the array?)

您可能想要使用Array的原型的concat()方法。 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/concat var alpha = ['a', 'b', 'c'], numeric = [1, 2, 3]; var alphaNumeric = alpha.concat(numeric); console.log(alphaNumeric); Br ...

读入输入流的N行并按相反顺序打印,而不使用数组或列表类型结构?(Read in N Lines of an Input Stream and print in reverse order without using array or list type structure?)

我认为你可以通过类似递归的方式来完成它: void printReversed(int n) { String line = reader.readLine(); if (n > 0) printReversed(n-1); System.out.println(line); } I think you can do it through recursion with something like: void printReversed(int n) { S ...

如何在不改变顺序的情况下将NSArrys插入NSDictonary(How to insert NSArrys into NSDictonary by without changing the order)

字典不是有序/索引的数据结构。 它是一个键/值配对数据结构。 您无法保证钥匙的顺序。 插入新密钥/值后,之前的订单可能会发生变化。 如果要使用字典,则需要使用键来访问值,而不是任何类型的索引。 A dictionary is not an ordered/indexed data structure. It is a key/value paired data structure. You can not guarantee the order of the keys. And after ins ...




  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)