测试视图模型组 [英] Unit testing the Viewmodel

查看:164
本文介绍了测试视图模型组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对TDD的排序新。我已经开始创作,我需要的视图模型作为纯自动财产的属性。

I am sort of new to TDD. I have started creating the properties I need on the view model as plain auto property.

public string Firstname { get; set; }

然后,我创建一个测试

Then I create a test

[TestMethod]
[Tag("Property")]
public void FirstNameTest()
{
    ViewModel = new CustomerViewModel();
    ViewModel.PropertyChanged += (s, e) =>
                                     {
                                         Assert.AreEqual("Firstname", e.PropertyName);
                                         Assert.AreEqual("Test", ViewModel.Firstname);
                                     };
    ViewModel.Firstname = "Test";
}

然后我会延长的实际执行情况,以使测试通过这样的:

Then I would extend the actual implementation to make the test pass like this:

public string Firstname
{
    get { return _contact.FirstName; }
    set
    {
        if (_contact.FirstName == value)
            return;

        _contact.FirstName = value;

        RaisePropertyChanged(() => Firstname);
    }
}

我的问题是,测试还通过对AUT财产。任何提示,我怎么能提高我的过程?

The problem I have is that test still passes for the Aut property. Any tip for me how i could improve my process?

推荐答案

您可以尝试写测试是异步的。考虑下面的测试方法:

You could try writing the test to be asynchronous. Consider this test method:

[TestMethod]
[Asynchronous]
public void TestMethod1()
{
    TestViewModel testViewModel = new TestViewModel();

    bool firstNameChanged = false;

    testViewModel.PropertyChanged +=
        (s, e) =>
            {
                if (e.PropertyName == "FirstName")
                {
                    firstNameChanged = true;
                }
            };

    EnqueueCallback(() => testViewModel.FirstName = "first name");
    EnqueueConditional(() => firstNameChanged == true);
    EnqueueTestComplete();
}

注意在该方法的顶部的异步属性。有两个重要的方法在这里:EnqueueCallback和EnqueueTestComplete。 EnqueueCallback将增加拉姆达EX pressions到队列中,测试方法将等到当前回调执行。在这里的情况,我们订阅的视图模型PropertyChanged事件,我们设置一个局部布尔变量设置为true时,名字属性通知的改变。然后我们排队两次回调:一个设置名字属性和一个断言,局部布尔变量发生了变化值。最后,我们需要添加一个呼叫到EnqueueTestComplete(),以使框架知道测试结束

Notice the Asynchronous attribute at the top of the method. There are two important methods here: EnqueueCallback and EnqueueTestComplete. EnqueueCallback will add lambda expressions to a queue and the test method will wait until the current callback is executed. In the case here, we subscribe to the PropertyChanged event on the ViewModel and we set a local boolean variable to true when the FirstName property notifies a change. We then Enqueue two callbacks: one to set the FirstName property and one to assert that the local boolean variable has changed value. Finally, we need to add a call to EnqueueTestComplete() so that the framework knows the test is over.

注:为了得到EnqueueCallback和EnqueueTestComplete,你需要从SilverlightTest继承你的测试类。您还需要导入Microsoft.Silverlight.Testing获得异步属性。它应该是这个样子:

NOTE: In order to get EnqueueCallback and EnqueueTestComplete, you need to inherit from SilverlightTest on your test class. You also need to import Microsoft.Silverlight.Testing to get the Asynchronous attribute. It should look something like this:

using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Foo.Example.Test
{
    [TestClass]
    public class Tests : SilverlightTest
    {

        // ... tests go here
    }
}

这篇关于测试视图模型组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆