Billpeng Space

技术源自生活
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

fakes的例子

Posted on 2013-03-11 18:11  billpeng  阅读(279)  评论(0编辑  收藏  举报

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bangso.Domain;
using Bangso.UI.Web.Controllers;
using System.Transactions;
using Bangso.Domain.UserInfos;

namespace Bangso.UnitTest
{
/// <summary>
/// ProductRepositroyTest 的摘要说明
/// </summary>
[TestClass]
public class UserInfoProductRepositoryTest : _UnitTestBase
{
public UserInfoProductRepositoryTest()
{

}

private TestContext testContextInstance;

/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

[TestMethod]
public void FindBy()
{
/*
* 使用Mock测试的原因是由于接口没有被实现,所以调用接口的类就无法被调试
* Mock的意义在于模拟接口的返回值,实际调试正确性的并不是接口,而是调用接口的类
*/

var database = new UserInfo { Username = "xinux" };
var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
{
FindByString = model => database
};

var xx = repository.FindByString("xinux");

Assert.AreEqual(xx.Username, "xinux");
}
}
}

 

 

 

---------------------------------------

 

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bangso.Domain;
using Bangso.UI.Web.Controllers;
using System.Transactions;
using Bangso.Domain.UserInfos;

namespace Bangso.UnitTest
{
/// <summary>
/// ProductRepositroyTest 的摘要说明
/// </summary>
[TestClass]
public class UserInfoProductRepositoryTest : _UnitTestBase
{
public UserInfoProductRepositoryTest()
{

}

private TestContext testContextInstance;

/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

//初始化数据
List<UserInfo> DataBase = new List<UserInfo>();
[TestInitialize()]
public void TestInitialize()
{
this.DataBase.Add(new UserInfo { Username = "xinux" });
}

[TestMethod]
public void FindBy()
{
var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
{
FindByString = model => this.DataBase[0]
};

var result = repository.FindByString("xinux");
Assert.AreEqual(result.Username, "xinux");
}

[TestMethod]
public void FindAll()
{
var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
{
FindAll = () => this.DataBase
};

var result = repository.FindAll();
Assert.AreEqual(result[0].Username, "xinux");
}

[TestMethod]
public void Add()
{
var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
{
AddUserInfo = (x) => { this.DataBase.Add(new UserInfo { Username = "bill"});
Assert.AreEqual(this.DataBase.Count, 2);
}
};
}

[TestMethod]
public void Update()
{
var database = new UserInfo { Username = "xinux" };
var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
{
UpdateUserInfoString = (x, y) =>
{
Assert.AreEqual(this.DataBase.Count, 2);
}
};
}


}
}