65.9K
CodeProject 正在变化。 阅读更多。
Home

单元测试 Silverlight ViewModel 样式模态弹出窗口

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.76/5 (9投票s)

May 2, 2010

Ms-PL

3分钟阅读

viewsIcon

54929

downloadIcon

769

ViewModel 风格的模态弹出窗口单元测试示例。

ViewModel 模态弹出窗口单元测试

实时示例

ViewModel 风格模式允许程序员创建一个完全没有 UI(用户界面)的应用程序。程序员只需要创建 ViewModelModel。完全没有编程能力的设计师则可以从一张白纸开始,在 Microsoft Expression Blend 4(或更高版本)中完全创建 View(UI)。如果您不熟悉 ViewModel 风格,建议您阅读 Silverlight ViewModel 风格:一个(过度)简化的解释 以获得介绍。

本文演示了如何对模态弹出窗口进行单元测试。

要了解如何创建弹出窗口,请参阅:MVVMPopUp.aspx

弹出窗口演示

让我们看一下弹出窗口演示的正常操作。

顶部有一个部分用于将输入的两个数字相加(注意:我刚意识到如果输入非数字会抛出错误,我应该在编写代码之前创建单元测试,这样就不会出现这种情况了! :))。

您可以选择主页面上的“默认值”并单击“显示弹出窗口”。

您在主页面上选择的“默认值”将设置在下拉列表中。您可以选择更改下拉列表中的值,然后单击“确定”、“取消”或右上角的“X”来关闭弹出窗口。

在弹出窗口的下拉列表中选择的值将显示在主页面的“弹出窗口结果”中。

单元测试 ViewModel 风格的模态弹出窗口

使用 ViewModel 风格,您可以直接在 ViewModel 中实例化一个弹出窗口并显示它。弹出窗口不被视为主视图页面的组成部分。就好像我们在暂时导航到另一个页面一样。主视图页面实际上对弹出窗口一无所知。它只知道它引发了一个 ICommand 并传递了一个默认值,然后稍后其页面上的一个属性不知何故被更改了(弹出窗口结果)。

如果您想测试弹出窗口,您只需要测试 ViewModel。

Silverlight 单元测试

在 Visual Studio 2010 中,我们可以通过选择“添加”>“新建项目...”>“Silverlight Unit Test Application”项目来添加一个 Silverlight 单元测试项目。

创建项目后,添加对 Microsoft.Expression.Interactions(否则会收到错误:“在类型‘Interaction’中找不到可附加属性‘Triggers’”)和要测试的 Silverlight 项目(MVVMPopUp)的引用。

接下来,打开您的 Tests.cs 文件并添加一些测试。

using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MVVMPopUp;

namespace SilverlightTest1
{
    [TestClass]
    public class Tests
    {
        [TestMethod]
        public void DoMathTestGood()
        {            
            // Create an instanace of the ViewModel
            MainViewModel VM = new MainViewModel();

            // Set the values
            VM.Value1Property = "100";
            VM.Value2Property = "100";

            // Perform calculation
            VM.DoMath(null);

            // We expect the result to be 200         
            Assert.AreEqual(VM.MathResultProperty, "200", true);
        }

        [TestMethod]
        public void TestPopupClickClose()
        {
            // Create an instanace of the ViewModel
            MainViewModel VM = new MainViewModel();

            // Set the Default value that will be passed to the Popup
            // Open the Popup
            VM.ShowPopUP("Yes");
            // Close the Popup
            VM.PopUP.Close();

            // Get the resut of the Popup
            string result = VM.SelectedPopUpValueProperty;

            // We passed Yes as the Default and we clicked the Close button 
            // So the expected result is "Yes - Cancel"
            Assert.AreEqual(result, "Yes - Cancel", true);
        }

        [TestMethod]
        public void TestPopupClickOK()
        {
            // Create an instanace of the ViewModel
            MainViewModel VM = new MainViewModel();

            // Set the Default value that will be passed to the Popup
            // Open the Popup
            VM.ShowPopUP("YES");

            // Close the Popup by Clicking OK

            // Get an instance of the ViewModel that the main ViewModel is using
            // for the Popup
            PopUpViewModel PopUpVM = (PopUpViewModel)VM.PopUP.DataContext;
            // Pass that to the Popups Set-up Command
            PopUpVM.SetPopUp(VM.PopUP);
            // Click the OK button
            PopUpVM.OKButton(null);

            // Get the resut of the Popup
            string result = VM.SelectedPopUpValueProperty;

            // We passed Yes as the Default and we clicked the Cancel button 
            // So the expected result is "Yes - Ok"
            Assert.AreEqual(result, "Yes - Ok", true);
        }

        [TestMethod]
        public void TestPopupClickCancel()
        {
            // Create an instanace of the ViewModel
            MainViewModel VM = new MainViewModel();

            // Set the Default value that will be passed to the Popup
            // Open the Popup
            VM.ShowPopUP("YES");

            // Close the Popup by Clicking Cancel

            // Get an instance of the ViewModel that the main ViewModel is using
            // for the Popup
            PopUpViewModel PopUpVM = (PopUpViewModel)VM.PopUP.DataContext;
            // Pass that to the Popups Set-up Command
            PopUpVM.SetPopUp(VM.PopUP);
            // Click the Cancel button
            PopUpVM.CancelButton(null);

            // Get the resut of the Popup
            string result = VM.SelectedPopUpValueProperty;

            // We passed Yes as the Default and we clicked the Cancel button 
            // So the expected result is "Yes - Cancel"
            Assert.AreEqual(result, "Yes - Cancel", true);
        }

        // If you run the test below, run use Ctrl+F5
        // (Start Without Debugging)

        //[TestMethod]
        //public void DoMathTestBad()
        //{
        //    // Create an instanace of the ViewModel
        //    MainViewModel VM = new MainViewModel();

        //    // Set the values
        //    VM.Value1Property = "100";
        //    VM.Value2Property = "200";

        //    // Perform calculation
        //    VM.DoMath(null);

        //    // We expect this test to fail
        //    Assert.AreEqual(VM.MathResultProperty, "99", true);
        //}
    }
}

运行测试

通过运行刚刚创建的 Silverlight Unit Test Application 来运行测试。

在网站项目中,右键单击创建 Silverlight Unit Test Application 项目时创建的测试页面,并将其设置为启动页。这也是在查看测试页面和查看正常应用程序之间切换的方式。当您想查看正常应用程序时,请将其页面设置为启动页。

Ctrl+F5(启动但不调试)来运行测试。如果您只使用 F5(运行并调试),则在测试失败时应用程序会停止。您可能需要调试才能实际调试您的测试,因此请使用适合您的方式。

当您运行测试项目时,您会看到一个出现的消息框,其中包含一个 5 秒倒计时器,还可以让您输入一个标签以仅运行某些测试。

测试将运行,您将看到输出。

代码隐藏代码有什么问题?

代码隐藏代码没有什么问题,我写了十多年了。然而,ViewModel 风格关注于创建没有设计师愿意触碰代码的应用程序。

如果我使用标准的代码隐藏,那么即使是将下拉列表更改为列表框也需要进行代码更改。通过不在代码隐藏中放置代码,设计师可以进行彻底的重新设计而无需更改代码

© . All rights reserved.