2016-10-06 8 views
6

में एक WPF टेक्स्टबॉक्स के लिए एक सेटटर को कैसे शामिल करें I NNnit में सिस्टम टेस्ट लिखने का प्रयास करें और मैं यूआई से यूआई ऑटोमेशन का उपयोग करके यूआई को आमंत्रित करना चाहता हूं।एनयूनीट टेस्ट

किसी कारण से मेरे आमंत्रण विफल हो गए - मुझे कुछ संकेत ऑनलाइन मिले जो मुझे एक ऐसे राज्य में पहुंचा जहां मैं संकलन परीक्षण लिख सकता हूं लेकिन मेरा दावा विफल हो जाता है।

यहां एक संकलित न्यूनतम उदाहरण है। मेरी समस्या उदाहरण में असफल परीक्षण है।

आवेदन XAML

<Application x:Class="InvokeTest.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:InvokeTest" 
      Startup="Application_Startup"/> 

आवेदन सीएस

using System.Windows; 

namespace InvokeTest 
{ 
    public partial class App : Application 
    { 
     private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      var view = new MainWindow(); 
      var viewmodel = new MainWindowViewModel(); 
      view.DataContext = viewmodel; 
      view.Show(); 
     } 
    } 
} 

विंडो XAML

<Window x:Class="InvokeTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:InvokeTest" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
<TextBox x:Name="MyTextBox" x:FieldModifier="public" Text="{Binding TextProperty, UpdateSourceTrigger=PropertyChanged}" /> 
</Window> 

विंडो सीएस

using NUnit.Framework; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows; 
using System.Windows.Automation.Peers; 
using System.Windows.Automation.Provider; 
using System.Windows.Controls; 

namespace InvokeTest 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class MainWindowViewModel 
    { 
     string textfield; 
     public string TextProperty 
     { 
      get { DebugLog("getter"); return textfield; } 
      set { textfield = value; DebugLog("setter"); } 
     } 

     private void DebugLog(string function) 
     { 
      Debug.WriteLine(ToString() + " " + nameof(TextProperty) + " " + function + " was called. value: '" + textfield ?? "<null>" + "'"); 
     } 

     [TestFixture, Apartment(ApartmentState.STA)] 
     public class WPFTest 
     { 
      MainWindow view; 
      MainWindowViewModel viewmodel; 

      [SetUp] 
      public void SetUp() 
      { 
       view = new MainWindow(); 
       viewmodel = new MainWindowViewModel(); 
       view.DataContext = viewmodel; 
      } 

      [Test] 
      public void SetTextBox_NoAutomation() 
      { 
       string expected = "I want to set this"; 
       view.MyTextBox.Text = expected; 
       Assert.AreEqual(expected, viewmodel.TextProperty); 
       /* 
       Test Name: SetTextBox_NoAutomation 
       Test Outcome: Failed 
       Result Message: 
       Expected: "I want to set this" 
       But was: null 
       */ 
      } 

      [Test] 
      public void SetTextBox_UIAutomation() 
      { 
       string expected = "I want to set this"; 
       SetValue(view.MyTextBox, expected); 
       Assert.AreEqual(expected, viewmodel.TextProperty); 
       /* 
       Test Name: SetTextBox_UIAutomation 
       Test Outcome: Failed 
       Result Message: 
       Expected: "I want to set this" 
       But was: null 
       */ 
      } 
      private static void SetValue(TextBox textbox, string value) 
      { 
       TextBoxAutomationPeer peer = new TextBoxAutomationPeer(textbox); 
       IValueProvider provider = peer.GetPattern(PatternInterface.Value) as IValueProvider; 
       provider.SetValue(value); 
      } 
     } 
    } 
} 

EDIT # 1: @Nkosi ने इंगित किया कि मेरे xaml
EDIT # 2 में बाध्यकारी विफलता थी: मैन्युअल परीक्षण को सक्षम करने के लिए बॉयलर का एक बिट जोड़ा गया और एक उपयोगकेस भी जोड़ा गया जो यूआईयूटोमेशन के बिना व्यवहार दिखाता है। यह सिर्फ एक sidenote है, uiautomation इस सवाल का मूल है।

+2

उम्मीद है कि आप इसे पहले ही जानते हैं, लेकिन यूनिट परीक्षण वास्तव में यूआई का परीक्षण करने के लिए डिज़ाइन नहीं किए गए हैं। यह शायद काम करने के लिए "हैक" हो सकता है, लेकिन जब आप जाते हैं तो आप सिस्टम से लड़ने लगेंगे। यूआई परीक्षण आमतौर पर कोडेड यूआई टेस्ट, और लिखित एकीकरण परीक्षण के हिस्से के रूप में किया जाता है। –

+0

@BradleyUffner हाँ मुझे पता है :) यह वास्तव में इतना आम है कि आप कोडेड यूआई टेस्ट पर मारने के बिना Google ui ऑटोमेशन नहीं कर सकते हैं। मुझे लगता है कि कोडित यूआई टेस्ट बेहतर हैं लेकिन मैं थोड़ी सी यूआईयूटोमेशन में गोता लगाने के लिए चाहता था। – Johannes

उत्तर

0

मुझे विंडो दिखाने की आवश्यकता है। मुझे लगता है कि संदेश पंप चलाना है।

अगर कोई इस पर विवरण दे सकता है तो मैं उस उत्तर को स्वीकृत उत्तर के रूप में सेट कर दूंगा।

using NUnit.Framework; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows; 
using System.Windows.Automation.Peers; 
using System.Windows.Automation.Provider; 
using System.Windows.Controls; 

namespace InvokeTest 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class MainWindowViewModel 
    { 
     string textfield; 
     public string TextProperty 
     { 
      get { DebugLog("getter"); return textfield; } 
      set { textfield = value; DebugLog("setter"); } 
     } 

     private void DebugLog(string function) 
     { 
      Debug.WriteLine(ToString() + " " + nameof(TextProperty) + " " + function + " was called. value: '" + textfield ?? "<null>" + "'"); 
     } 

     [TestFixture, Apartment(ApartmentState.STA)] 
     public class WPFTest 
     { 
      MainWindow view; 
      MainWindowViewModel viewmodel; 

      [SetUp] 
      public void SetUp() 
      { 
       view = new MainWindow(); 
       viewmodel = new MainWindowViewModel(); 
       view.DataContext = viewmodel; 
       view.Show(); 
      } 

      [TearDown] 
      public void TearDown() 
      { 
       view.Close(); 
       view.DataContext = null; 
       view = null; 
       viewmodel = null; 
      } 

      [Test] 
      public void SetTextBox_NoAutomation() 
      { 
       string expected = "I want to set this"; 
       view.MyTextBox.Text = expected; 
       Assert.AreEqual(expected, viewmodel.TextProperty); 
      } 

      [Test] 
      public void SetTextBox_UIAutomation() 
      { 
       string expected = "I want to set this"; 
       SetValue(view.MyTextBox, expected); 
       Assert.AreEqual(expected, viewmodel.TextProperty); 
      } 

      private void SetValue(TextBox textbox, string value) 
      { 
       TextBoxAutomationPeer peer = new TextBoxAutomationPeer(textbox); 
       IValueProvider provider = peer.GetPattern(PatternInterface.Value) as IValueProvider; 
       provider.SetValue(value); 
      } 
     } 
    } 
} 
+1

मुझे लगता है कि मूल संस्करण है कि बाइंडिंग AttachToContext नहीं है। [बाध्यकारी पहली बार पहली बार हल हो जाएगा जब UpdateLayout विधि पहली बार कॉल की जाती है, यदि DataContext पहले से सेट है।] (Http: // stackoverflow।कॉम/प्रश्न/13875537/कब-डेटा-बाइंडिंग-लागू) दूसरा संस्करण दृश्य। शो अपडेटलेआउट ईवेंट को ट्रिगर करेगा। – zzczzc004

2

असल में आप TextBox.Text Property पर कॉल कर सकते हैं।

view.MyTextBox.Text = expected; 

आपके विचार में आप भी अपने दृश्य मॉडल पर Text संपत्ति के लिए बाध्य कर रहे हैं, जबकि अपने परीक्षण में दृश्य मॉडल एक MyTextBox संपत्ति है। मिलान करने के लिए एक या दूसरे को अपडेट करने की आवश्यकता है।

public class MainWindowViewModel 
{ 
    public string Text { get; set; } 
} 

[TestFixture, Apartment(ApartmentState.STA)] 
public class WPFTest 
{ 
    [Test] 
    public void SetTextBox() 
    { 
     //Arrange 
     var expected = "I want to set this"; 

     var view = new MainWindow(); 
     var viewmodel = new MainWindowViewModel(); 
     view.DataContext = viewmodel; 

     //Act 
     view.MyTextBox.Text = expected; 

     //Assert 
     Assert.AreEqual(expected, viewmodel.Text); 
    } 
} 
+0

मैंने बाध्यकारी विफलता दायर की (मुझे लगता है :))। आपका सुझाव मेरी मशीन पर काम नहीं करता है - क्या आप इसे चला सकते हैं? – Johannes

+0

क्या आपको कोई त्रुटि मिल रही है? यदि ऐसा है, तो वो क्या हैं? – Nkosi

+0

दावे के अलावा अन्य? नहीं। – Johannes

संबंधित मुद्दे