为什么证书对话框在单元测试中不显示 [英] Why Certificate Dialogs Don't Show in Unit Tests
问题描述
大家好,
我发现了似乎是一个错误的错误,但不确定.我有几个从VS 2010移植的单元测试,其中一些提示用户提示证书凭据(这意味着选择证书并授予单元测试许可以加密/解密数据).
I found what seems to be a bug, but I'm not sure. I have several Unit tests that I ported from VS 2010 and some of them prompt the user for certificate credentials (meaning choosing a cert and granting the Unit Test permission to sing/decrypt data).
我的问题是,我正在使用VS 2012,该操作系统在Windows 2012上运行,我不再看到该对话框,并且测试方法会无限期地暂停.完全没有显示错误信息.
My issue is now that I am using VS 2012, running on Windows 2012, I don't see the dialog anymore and the test method halts indefinitely. No error information is shown at all.
您知道为什么这种新行为会在VS 2012和/或Windows 2012中存在吗?
Any idea why this new behavior exists in VS 2012 and/or Windows 2012?
示例代码
Example Code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography.X509Certificates;
namespace UnitTestProject1 {
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var selectedCertificates = X509Certificate2UI
.SelectFromCollection(store.Certificates, "Select Certificate", "Please select a certificate.", X509SelectionFlag.SingleSelection);
// never gets here because UI is frozen
Assert.IsTrue(selectedCertificates.Count > 0);
}
}
}
假设
- 您的我的商店"中至少有一个证书
- 您已将System.Security.dll添加到单元测试项目中
-拉沙德·里维拉(Rashad Rivera)www.omegusprime.com
- Rashad Rivera www.omegusprime.com
推荐答案
我怀疑新的单元测试框架是如何隐藏新对话框的,但是却不知道为什么会这样. t隐藏常规的MessageBox.Show(...).所以我想,如果我创建一个WPF窗口,显示它并传递该窗口的HWND值,该怎么办? 到SelectFromCollection方法.信不信由你,它行得通.参见下面的修改代码
I had a suspition that the new Unit Test Framework was some how hiding the new dialog, but could not understand why it won't hide a regular MessageBox.Show(...). So I figured, what if I created a WPF window, show it and pass the window's HWND value to the SelectFromCollection method. Believe it or not, it works. See modified code below
新假设
- 您将PresentationCore,PresentationFramework,System.Xaml和WindowsBase程序集添加到单元测试项目中.
新代码块
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography.X509Certificates;
using System.Windows;
using System.Windows.Interop;
using System.Threading;
namespace UnitTestProject1 {
[TestClass]
public class UnitTest1 {
X509Certificate2 m_UserCert;
void ShowWindow() {
var app = new Application();
var wnd = new Window();
wnd.Loaded += (s, e) => {
var hwnd = new WindowInteropHelper(wnd).Handle;
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var selectedCertificates = X509Certificate2UI
.SelectFromCollection(store.Certificates, "Select Certificate", "Please select a certificate.", X509SelectionFlag.SingleSelection, hwnd);
m_UserCert = selectedCertificates[0];
wnd.Close();
//app.Shutdown(0);
};
var returnValue = app.Run(wnd);
returnValue.ToString();
}
[TestMethod]
public void TestMethod1() {
var t = new Thread(this.ShowWindow);
t.SetApartmentState(ApartmentState.STA);
t.Start();
do {
Thread.Sleep(TimeSpan.FromSeconds(1));
} while (t.IsAlive);
// never gets here because UI is frozen
Assert.IsNotNull(m_UserCert);
}
}
}
我仍然想知道是否可以将单元测试配置为不隐藏窗口,还是希望在将其提交给connect.microsoft.com之前获得作为错误或通过设计得到的确认.谢谢.
这篇关于为什么证书对话框在单元测试中不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!