你怎么称呼Robolectric的ShadowWifiManager.setScanResults()?

时间:2016-08-01 18:16:18

标签: android unit-testing robolectric

在测试期间,我需要使WifiManager.getScanResults()方法返回非空列表。我正在使用Robolectric。我发现ShadowWifiManagersetScanResults()方法,它采用了ScanResult个对象的列表 - 但它们没有公共构造函数。

1 个答案:

答案 0 :(得分:2)

我发现 Robolectric 的阴影为ScanResult,也有newInstance方法。它可以像这样使用:

shadowOf(((WifiManager)controller.get().getSystemService(Context.WIFI_SERVICE))).setScanResults(Collections.singletonList(ShadowScanResult.newInstance("Foo", "Bar", "", 1, 2)));

或者,更详细地说: final int level = 5; final int frequency = 100; final ScanResult scanResult = ShadowScanResult.newInstance( "A fake SSID", "A fake BSSID", "Some capabilities", level, frequency); final Context context = controller.get(); final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); shadowOf(wifiManager).setScanResults(Collections.singletonList(scanResult));