在我的application_controller.rb中,我有一行代码如下:
@Override
public void onFragmentInteraction(ArrayList<Songfileinfo> songarr, int position) {
playAudio(songarr, position);
Log.v(""+songarr.size(),"I GOT THE SIZE");
}
private void playAudio(ArrayList<Songfileinfo> arrayList, int audioIndex) {
//Check is service is active
if (!serviceBound) {
//Store Serializable audioList to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
Intent playerIntent = new Intent(this, MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Store the new audioIndex to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
//Service is active
//Send a broadcast to the service -> PLAY_NEW_AUDIO
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
}
}
在我的rspec代码中,我有一行代码访问application_controller的索引路径,如下所示
def index
CaseStatus.order(:application_source).pluck(:application_source).uniq!
end
当我直接运行代码时,它运行得很好但是当它通过rspec访问application_controller.rb时,我收到一个错误,上面写着
visit applications_path
不确定虽然我通过rspec和capybara得到此错误,但如果我运行代码
NoMethodError:
undefined method `compact' for nil:NilClass
完美执行,没有错误。有点混淆了代码中的def index
CaseStatus.order(:application_source).pluck(:application_source)
end
突破,结果突然变为零。
我收到此错误
uniq!
答案 0 :(得分:0)
我不认为uniq!
是您想要在这种情况下使用的方法,请参阅:
如果未进行任何更改,则返回nil(即,未找到重复项)。 https://ruby-doc.org/core-2.2.0/Array.html#method-i-uniq-21
所以它的工作原理如下:
2.3.1 :008 > a = [1,2,3,3,nil].uniq!
=> [1, 2, 3, nil]
2.3.1 :009 > a = [1,2,3,nil].uniq!
=> nil
2.3.1 :010 >
另一方面, uniq
的作用类似于:
2.3.1 :010 > a = [1,2,3,3,nil].uniq
=> [1, 2, 3, nil]
2.3.1 :011 > a = [1,2,3,nil].uniq
=> [1, 2, 3, nil]
并在uniq
的输出上运行compact
以删除nil
值是安全的。