【发布时间】:2012-01-14 22:37:23
【问题描述】:
我是 Pex 和 Moles 的新手。我正在尝试在我的代码上运行 Pex,但我正在使用构造函数注入。有没有办法指导 Pex 如何注入构造函数?
编辑
public UserLogic(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public int GetUsersAge(int id)
{
User user = _userRepository.GetById(id);
DateTime DOB = user.DOB;
DateTime now = DateTime.Today;
int age = now.Year - DOB.Year;
if (DOB > now.AddYears(-age)) age--;
return age;
}
我需要注入一个存根 userRepository。调用 _userRepository.GetById(id) 时,Pex 失败并出现 NullReferenceException。我一直在使用 Moq 进行单元测试,但我想切换到 pex 和 moles
我应该使用 PexFactories 创建我的存根吗?
【问题讨论】:
-
显示一些代码,你可以用痣替换任何代码。
-
这并不完全正确,在某些情况下您将无法使用 Moles(例如social.msdn.microsoft.com/Forums/en-US/pex/thread/…)
-
还有一件事,我认为如果您有机会从头开始设计您的应用程序,只要您正确地接口等,您通常应该可以使用 MOQ。 Moles 可以帮助处理您可能无法/允许/......再更改的遗留代码。
标签: moles pex pex-and-moles