编写综合的单元测试
作者:网络转载 发布时间:[ 2012/7/3 11:15:57 ] 推荐标签:
当我们执行这个测试时,会得到一个失败提示信息“期望的属性名‘FirstName’,但接收到的是’IsChanged’”。显然,设置FirstName的属性触发了“IsChanged”标记,我们需要把它考虑在内。因此我们把它加入:
[TestMethod]
public voidPerson_FirstName_Set_PropertyChanged()
{
varperson =newPerson("Adam","Smith");
vareventAssert =newGranite.Testing.PropertyChangedEventAssert(person);
person.FirstName ="Bob";
eventAssert.SkipEvent();//this was IsChanged
eventAssert.Expect("FirstName");
}
鉴于以上两个测试,我们考虑当FirstName被修改时还有其他什么属性会改变。查看API,IsChanged和FullName属性会变化。
[TestMethod]
public voidPerson_FullName_Changed_By_Setting_FirstName()
{
varperson =newPerson("Adam","Smith");
person.FirstName ="Bob";
Assert.AreEqual("Bob Smith", person.FullName);
}
[TestMethod]
public voidPerson_IsChanged_Changed_By_Setting_FirstName() { varperson =newPerson("Adam","Smith");
person.FirstName ="Bob";
Assert.IsTrue(person.IsChanged);
}
当然,如果这些属性改变了,我们需要获取到属性改变通知:
[TestMethod]
public void Person_IsChanged_Property_Change_Notification_By_Setting_FirstName()
{
var person = new Person("Adam", "Smith");
var eventAssert = new PropertyChangedEventAssert(person);
person.FirstName = "Bob";
eventAssert.Expect("IsChanged");
}
[TestMethod]
public void Person_FullName_Property_Change_Notification_By_Setting_FirstName()
{
var person = new Person("Adam", "Smith");
var eventAssert = new PropertyChangedEventAssert(person);
person.FirstName = "Bob";
eventAssert.SkipEvent(); //this was IsChanged
eventAssert.SkipEvent(); //this was FirstName
eventAssert.Expect("FullName");
}
接下来两个测试针对HasErrors这个属性和ErrorsChanged事件。
[TestMethod]
public void Person_FirstName_Set_HasErrorsIsFalse()
{
var person = new Person("Adam", "Smith");
person.FirstName = "Bob";
Assert.IsFalse(person.HasErrors);
}
[TestMethod]
public void Person_FirstName_Set_ErrorsChanged_Did_Not_Fire()
{
var person = new Person("Adam", "Smith");
var errorsChangedAssert = new ErrorsChangedEventAssert(person);
person.FirstName = "Bob";
errorsChangedAssert.ExpectNothing();
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11