积极答复者
如何对多线程中的fakes进行单元测试?

问题
-
请问如下的代码如何进行单元测试?
private void Initialize() { Task.Factory.StartNew(() => { Parallel.Invoke(() => { KinectSensor kinectSensor = null; // while haven't got the sensor, keep fetching while (kinectSensor == null) { Thread.Sleep(200); kinectSensor = (from sensor in KinectSensor.KinectSensors where sensor.Status == KinectStatus.Connected select sensor).FirstOrDefault(); } _kinectService = new KinectService(kinectSensor); _kinectService.SetupKinectSensor(new EventHandler<ColorImageFrameReadyEventArgs>(ColorImageFrameReadyEventHandler), new EventHandler<SkeletonFrameReadyEventArgs>(SkeletonFrameReadyEventHandler)); DispatcherHelper.CheckBeginInvokeOnUI(() => { StatusDescription = "Kinect Has connected"; }); }, () => { IMCU mcu = null; while (mcu == null) { Thread.Sleep(200); // get mcu here } _mcuService = new MCUService(mcu); DispatcherHelper.CheckBeginInvokeOnUI(() => { StatusDescription = "MCU Has connected"; }); }); DispatcherHelper.CheckBeginInvokeOnUI(() => { CameraShadowColor = "#FF66B034"; StatusColor = "#FF66B034"; StatusCaption = "Ready"; StatusDescription = "Ready to go"; ButtonString = "OK"; }); }); }
- 已移动 Franklin ChenMicrosoft employee 2014年10月17日 9:43 VSTS question
答案
-
根据后来我的测试,好像没有办法进行测试。。。用stub肯定不行,因为没法创建KinectSensor的实例。而用shim也不行因为他里面有一个静态属性,是ReadOnlyCollection类型的,这个在mscorlib里面,也没法用shim进行模拟,所以好像这个根本没法进行测试。。。
如果这样,我建议你到产品组提交一个feedback:
http://connect.microsoft.com/VisualStudio/feedback/CreateFeedback.aspx
最好用英文提交,产品组会认真研究你的问题并给出相应的建议。如果你提交了反馈,你可以把链接分享给我们,这样我们也能从产品组得到最新的消息。
谢谢您的合作与支持!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 Jack Zhai-MSFTMicrosoft contingent staff, Moderator 2014年10月27日 1:45
全部回复
-
您好,
在这之前,我想我们需要考虑的是我们是用哪种方法(存根还是填充码)或者怎么样创建相应的测试?
http://msdn.microsoft.com/zh-CN/library/hh549175.aspx
Objective | Consideration
Stub
Shim
Looking for maximum performance?
a
a (slower)
Abstract and Virtual methods
a
Interfaces
a
Internal types
a
a
Static methods
a
Sealed types
a
Private methods
a
这是一个例子,它分享了详细的创建这种类型测试的步骤:
http://www.codeproject.com/Articles/582812/Unit-testing-with-Fakes-with-Visual-studio-Premium
希望能帮到你!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Jack Zhai-MSFTMicrosoft contingent staff, Moderator 2014年10月20日 8:17
-
根据后来我的测试,好像没有办法进行测试。。。用stub肯定不行,因为没法创建KinectSensor的实例。而用shim也不行因为他里面有一个静态属性,是ReadOnlyCollection类型的,这个在mscorlib里面,也没法用shim进行模拟,所以好像这个根本没法进行测试。。。
如果这样,我建议你到产品组提交一个feedback:
http://connect.microsoft.com/VisualStudio/feedback/CreateFeedback.aspx
最好用英文提交,产品组会认真研究你的问题并给出相应的建议。如果你提交了反馈,你可以把链接分享给我们,这样我们也能从产品组得到最新的消息。
谢谢您的合作与支持!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 Jack Zhai-MSFTMicrosoft contingent staff, Moderator 2014年10月27日 1:45