2009-04-14 40 views
11

Tôi có một cửa sổ đơn giản với tham chiếu đến một StaticResource trong App.xaml.Kiểm tra cửa sổ WPF với StaticResources

định nghĩa tài nguyên App.xaml:

<!-- Standard Text Box Style --> 
<Style x:Key="textBoxStyleStd" TargetType="{x:Type TextBox}"> 
    <Setter Property="FontSize" Value="14" /> 
</Style> 

componets Window bằng cách sử dụng tài nguyên:

<TextBlock Grid.Column="1" Grid.Row="0" Name="stationIdTitle" 
      Style="{StaticResource textBlockStyleStd}" 
      VerticalAlignment="Center" HorizontalAlignment="Center" 
      Text="{LocText Key=Title, Dict={StaticResource Dictionary}, 
       Assembly={StaticResource Assembly}}"/> 

Khi cố gắng để kiểm tra đơn vị Window này tôi nhận được lỗi:

System.Windows.Markup.XamlParseException: Cannot find resource named '{textBlockStyleStd}'. Resource names are case sensitive. Error at object 'stationIdTitle' in markup file 'Zpg;component/guicomponenets/screens/enterstationidscreen.xaml' Line 23 Position 71.

Is có cách nào xung quanh điều này? Mã kiểm tra đơn vị của tôi là:

[Test] 
public void TestEnterKeyPressedNoText() 
{ 
    IPickingBusinessObject pickingBusinessObject = mock.StrictMock<IPickingBusinessObject>(); 

    EnterStationIdScreen objectUnderTest = new EnterStationIdScreen(pickingBusinessObject); 

    Assert.AreEqual(Visibility.Visible, objectUnderTest.stationIdError.Visibility); 

    Assert.AreEqual("werwe", "oksdf"); 

    Replay(); 

    objectUnderTest.EnterKeyPressed(); 

    Verify(); 
} 
+0

Giải pháp có thể có thể thay thế StaticResource với liên kết DynamicResource – Boogier

Trả lời

12

Cảm ơn Kent,

Tôi nhìn đề xuất của bạn và trong hầu hết các kịch bản tôi đồng ý mô hình nên được sử dụng và thử nghiệm tuy nhiên, có một số mã đi kèm với các điều khiển (ví dụ TextBox tầm nhìn) Tôi vẫn muốn thử nghiệm. Để làm tròn điều này, bạn có thể tạo một cá thể của Ứng dụng của bạn (nhưng không khởi tạo nó) và thêm các tài nguyên theo cách thủ công. Điều này dẫn đến trùng lặp trong App.xaml và thử nghiệm đơn vị cơ bản nhưng điều này cho phép tôi hoàn thành các bài kiểm tra mà tôi muốn.

 if (Application.Current == null) 
     { 
      App application = new App(); 

      #region Add Static Resources from the App.xaml 

      Style textBoxStyle = new Style(typeof(TextBox)); 
      textBoxStyle.Setters.Add(new Setter(TextBox.FontSizeProperty, 14d)); 

      Style textBlockStyle = new Style(typeof(TextBlock)); 
      textBlockStyle.Setters.Add(new Setter(TextBlock.FontSizeProperty, 14d)); 

      application.Resources.Add("TextBoxStyleStd", textBoxStyle); 
      application.Resources.Add("TextBlockStyleStd", textBlockStyle); 
      application.Resources.Add("TextBlockStyleError", textBlockStyle); 
      application.Resources.Add("Assembly", "Zpg"); 

      #endregion 
     }  
+0

+1 cos điều này đã giúp thoát khỏi một liên kết tương tự .. cảm ơn! – PaulJ

+4

có vấn đề tương tự, chỉ cần gọi app.InitializeComponent() bên cạnh tạo cá thể. các từ điển tài nguyên sẽ được phổ biến. Infact thats những gì phương pháp chính hiện. Đây là đoạn trích từ phương thức chính, bạn có thể bỏ qua ứng dụng.Chúng tôi là đơn vị thử nghiệm ở đây. [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] void tĩnh công cộng Main() { WPFComboBox.App app = new WPFComboBox.App(); app.InitializeComponent(); ứng dụng.Chạy(); } – ioWint

+1

Ngoài ra, nếu bạn đang sử dụng tài nguyên tĩnh để căn cứ 'Kiểu' của UserControl' theo mặc định, chẳng hạn như '