简体   繁体   English

单元测试 Microsoft Band

[英]Unit testing Microsoft Band

Trying to do unit testing using MSTest in VS2015 with the Microsoft Band nuGet package and running into the following error尝试使用 VS2015 中的 MSTest 和 Microsoft Band nuGet 包进行单元测试并遇到以下错误

"Microsoft.Band.BandIOException: An error occurred while attempting to acquire the Bluetooth device service.
This error can occur if the paired device is unreachable or has become unpaired from the current host. 
System.InvalidOperationException: A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)".

Code runs fine when run inside the application.代码在应用程序内部运行时运行良好。 It fails on the line to call BandClientManager.Instance.ConnectAsync .它无法在线调用BandClientManager.Instance.ConnectAsync

The exception and error message are not helpful here, but you must establish Bluetooth connections on a UI thread.异常和错误消息在这里没有帮助,但您必须在 UI 线程上建立蓝牙连接。 This is because the app might prompt the user and ask if they want to allow access to the Bluetooth device.这是因为应用程序可能会提示用户并询问他们是否允许访问蓝牙设备。

For example, in a UWP app, you can do the following to ensure UI thread execution:例如,在 UWP 应用中,您可以执行以下操作以确保 UI 线程执行:

await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
    IBandClient client = await BandClientManager.Instance.ConnectAsync(...);
    ...
});

Alternatively, if you have access to a UI control, you can use its Dispatcher directly.或者,如果您有权访问 UI 控件,则可以直接使用其Dispatcher

Any code that ultimately calls BluetoothLEDevice.FromBluetoothAddressAsync must do it on a UI thread.任何最终调用BluetoothLEDevice.FromBluetoothAddressAsync代码都必须在 UI 线程上执行。 The Bluetooth access prompt will happen whenever the app package manifest ( .appxmanifest ) changes.每当应用程序包清单 ( .appxmanifest ) 更改时,就会出现蓝牙访问提示。

I can't imagine this fix being dependable for unit tests since there's no UI.由于没有 UI,我无法想象此修复程序对于单元测试是可靠的。 I'm not sure what the intended fix is besides mocking the client interfaces and just avoiding Bluetooth altogether.除了模拟客户端接口和完全避免蓝牙之外,我不确定预期的修复是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM