【发布时间】:2013-03-19 20:57:38
【问题描述】:
我正在尝试为 ServiceStack 服务编写简单的单元测试,我正在通过他们在线的测试和这里的几个线程。这是我想要完成的大部分细节的主线程 - Unit Test HTTPRequest Headers with ServiceStack。
但是,我在将 IDbConnection 对象注入服务时遇到了问题。在 web 方法中,使用 OrmLite 的 GetDictionary 方法填充字典对象。但我无法模拟它,因为 GetDictionary 是扩展方法。
private Mock<IDbConnection> _dbConnectionMock;
private Dictionary<string, string> _nameValuePairs;
[SetUp]
public void SetUp()
{
_dbConnectionMock = new Mock<IDbConnection>();
_nameValuePairs = new Dictionary<string, string>()
{
{"name","test"},
{"Updatedby", "5/23/12 7:00:15 AM"},
{"Address", "212 Adam St."}
};
}
在测试方法中
var service = new CustomerLookupService(_dbConnectionMock.Object);
var response = (HttpResult)service.Any(new CustomerLookup { name = "test" });
//assert statements
如果GetDictionary方法不能被mock,我什至愿意调用命中DB的web方法,为此我需要创建AppHost。
【问题讨论】:
标签: unit-testing nunit moq servicestack