Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4972808
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: 嵌入式

2012-01-17 17:40:44

转自: http://www.cnblogs.com/midshipman/archive/2010/11/11.html

既然要用MVVM和TDD,两样利器必不可少:

1.

2.

都有相应的WP7版本,添加dll到项目中即可。

 

下面大概说下要点:

1. 创建一个新项目WeiBo7
2. 添加一个测试项目WeiBo7.Test

1)添加 Microsoft.Silverlight.Testing & Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight到项目reference中
2)修改MainPage()如下

代码
// Constructor
public MainPage()
{
InitializeComponent();

// set up unit testing
Content = UnitTestSystem.CreateTestPage();
IMobileTestPage imtp
= Content as IMobileTestPage;

if (imtp != null)
{
BackKeyPress
+= (x, xe) => xe.Cancel = imtp.NavigateBack();
}
}

3)创建一个新类MainViewModelTests 

代码
using System;
using System.Net;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WeiBo7.ViewModels;


namespace WeiBo7.Test
{
[TestClass]
public class MainViewModelTests : SilverlightTest
{

}
}

4)添加一个新的测试方法

代码
[TestMethod]
public void IsHomeRefreshing_SetValue_RaisesPropertyChanged()
{
var mainViewModel
= new MainViewModel();

bool propChanged = false;
mainViewModel.PropertyChanged
+=
(s, e)
=>
{
if (e.PropertyName == "IsHomeRefreshing")
{
propChanged
= true;
}
};

mainViewModel.IsHomeRefreshing
= !mainViewModel.IsHomeRefreshing;
Assert.IsTrue(propChanged);
}

3. 添加一个ViewModelBase类,所有的viewmodel都要从它继承

代码
using System.ComponentModel;

namespace WeiBo7.ViewModels
{
public class ViewModelBase
{
protected void OnNotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(
this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;

}
}

4. 在WeiBo7中添加MainViewModel类并加入一个属性IsHomeRefreshing, 这是用来绑定ProgressBar的,目前UI还没创建,但已经可以测试它了!

代码
private bool _isHomeRefreshing = false;
///
/// Indicates whether there is an indeterminate operation in progress
///

public bool IsHomeRefreshing
{
get { return _isHomeRefreshing; }
set
{
if (value != _isHomeRefreshing)
{
_isHomeRefreshing
= value;
OnNotifyPropertyChanged(
"IsHomeRefreshing");
}
}
}

如果没什么问题的话,运行测试项目,你会看到如下结果:

更详细的结果:

阅读(941) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~