日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WPF單元測試方法總結(jié)

WPF單元測試的創(chuàng)建需要有一定編程經(jīng)驗的開發(fā)人員,才能熟練的操作。那么,在這篇文章中我們將針對WPF單元測試的創(chuàng)建過程做一個簡單的介紹。 #t#

樺南ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

1,對普通類(非WPF UI組件)進(jìn)行測試:

這和在.Net2.0中使用NUnit進(jìn)行測試時一樣,不會出現(xiàn)任何問題,參考下面的代碼:

  1. [TestFixture]   
  2. public class ClassTest {   
  3. [Test] public void TestRun() {   
  4. ClassLibrary1.Class1 obj = 
    new ClassLibrary1.Class1();  
  5. double expected = 9;   
  6. double result = obj.GetSomeValue(3);   
  7. Assert.AreEqual(expected, result);   
  8. }   

2,對WPF UI組件進(jìn)行測試

使用NUnit對WPF UI組件(比如MyWindow,MyUserControl)進(jìn)行測試的時候,NUnit會報如下異常:“The calling thread must be STA, because many UI components require this”。

下面是錯誤的WPF單元測試代碼:

 
 
 
  1. [TestFixture]   
  2. public class ClassTest {   
  3. [Test] public void TestRun() 
    { WindowsApplication1.Window1 obj = 
    new WindowsApplication1.Window1();   
  4. double expected = 9;   
  5. double result = obj.GetSomeValue(3);   
  6. Assert.AreEqual(expected, result);   
  7. }   

為了讓調(diào)用線程為STA,我們可以編寫一個輔助類CrossThreadTestRunner:

 
 
 
  1. using System; using System.
    Collections.Generic;   
  2. using System.Text;   
  3. using System.Threading;   
  4. using System.Security.Permissions;   
  5. using System.Reflection;   
  6. namespace TestUnit {   
  7. public class CrossThreadTestRunner {   
  8. private Exception lastException;   
  9. public void RunInMTA(ThreadStart 
    userDelegate) {   
  10. Run(userDelegate, ApartmentState.MTA);   
  11. }   
  12. public void RunInSTA(ThreadStart 
    userDelegate) {   
  13. Run(userDelegate, ApartmentState.STA);   
  14. }   
  15. private void Run(ThreadStart 
    userDelegate, 
    ApartmentState apartmentState) {   
  16. lastException = null;   
  17. Thread thread = new Thread( delegate() {   
  18. try { userDelegate.Invoke();   
  19. }   
  20. catch (Exception e) { lastException = e;   
  21. }   
  22. });   
  23. thread.SetApartmentState(apartmentState);   
  24. thread.Start();   
  25. thread.Join();   
  26. if (ExceptionWasThrown())   
  27. ThrowExceptionPreservingStack
    (lastException);   
  28. }   
  29. private bool ExceptionWasThrown() {   
  30. return lastException != null;   
  31. }   
  32. [ReflectionPermission(Security
    Action.Demand)]   
  33. private static void ThrowException
    PreservingStack(Exception exception) {   
  34. FieldInfo remoteStackTraceString = 
    typeof(Exception).GetField( "_remoteStack
    TraceString", BindingFlags.Instance | 
    BindingFlags.NonPublic);  
  35. remoteStackTraceString.SetValue(exception,
     exception.StackTrace + Environment.NewLine);   
  36. throw exception;   
  37. }   
  38. }   

并編寫正確的WPF單元測試代碼:

 
 
 
  1. [TestFixture] public class ClassTest {   
  2. [Test] public void TestRun() {   
  3. CrossThreadTestRunner runner = 
    new CrossThreadTestRunner();   
  4. runner.RunInSTA( delegate {   
  5. Console.WriteLine(Thread.CurrentThread.
    GetApartmentState());   
  6. WindowsApplication1.Window1 obj = 
    new WindowsApplication1.Window1(); 
    double expected = 9;  
  7. double result = obj.GetSomeValue(3);   
  8. Assert.AreEqual(expected, result);   
  9. });   
  10. }   

另外,使用NUnit時,您需要添加對nunit.framework.dll的引用,并對WPF單元測試類添加[TestFixture]屬性標(biāo)記以及對測試方法添加[Test]屬性標(biāo)記,然后將生成的程序集用nunit.exe打開就可以了。


本文名稱:WPF單元測試方法總結(jié)
本文來源:http://m.5511xx.com/article/dhjpiho.html