问题描述
我正在尝试测试我的方法,该方法接受哈希并重新排序。我目前有:
def sort_views
help = view_counter.sort_by { |route,length| length }.reverse.to_h
p help #just so I can see the output in the test
end
然后我进行测试:
describe "#sort_views" do
let(:result) do {
"/help_page/1" => 3,"/about/2" => 1,"/contact" => 1,"/home" => 1,"/index" => 2,} end
it "then sorts them via the view count" do
expect(subject.sort_views).to be(result)
end
end
我的问题是测试通过了...。但是,我目前故意在结果中输入错误的订单。然而,该方法中的p帮助正确地对其进行了重新排序,因此输出实际上是不同的。我已经尝试过eq和eql,但是我相信它们只是测试结构?我知道平等是行不通的...
对于上下文,我的p帮助输出:{"/help_page/1"=>3,"/index"=>2,"/about/2"=>1,"/home"=>1,"/contact"=>1}
,运行测试时。
所以我有一种方法也可以说在测试中测试结果哈希的顺序,而无需调用.sort_by {| route,length |长度} .reverse.to_h也放在我的结果变量上?
解决方法
这是因为散列相同,请参见this post。哈希实际上没有顺序的概念,当您调用Launching lib\main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Checking the license for package Android SDK Platform 29 in C:\Program Files (x86)\Android\android-sdk\licenses
License for package Android SDK Platform 29 accepted.
Preparing "Install Android SDK Platform 29 (revision: 5)".
Warning: Failed to read or create install properties file.
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Failed to install the following SDK components:
platforms;android-29 Android SDK Platform 29
The SDK directory is not writable (C:\Program Files (x86)\Android\android-sdk)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 14s
Exception: Gradle task assembleDebug failed with exit code 1
方法时,它会将数据转换为数组,对数组进行排序,然后返回数组。如果将数组转换为哈希,则实际上会失去顺序。
如果您关心这里的顺序,请删除sort_by
,然后将数据作为数组处理。然后您的测试应该可以工作。
您可以在测试中使用to_h
,并且不需要反向排序。
.to_a