这是一个艰难的问题,因为使用 Pex 和 Moles 的人并不多,我认为(尽管 Pex 是一个非常棒的产品 - 比任何其他单元测试工具都要好得多)
我有一个数据项目,它有一个非常简单的模型,只有一个实体(DBItem
)。我还在DBRepository
这个项目中编写了一个操作这个 EF 模型的代码。Repository 有一个称为GetItems()
返回业务层项目列表 ( BLItem
) 的方法,看起来与此类似(简化示例):
public IList<BLItem> GetItems()
{
using (var ctx = new EFContext("name=MyWebConfigConnectionName"))
{
DateTime limit = DateTime.Today.AddDays(-10);
IList<DBItem> result = ctx.Items.Where(i => i.Changed > limit).ToList();
return result.ConvertAll(i => i.ToBusinessObject());
}
}
所以现在我想为这个特定的方法创建一些单元测试。我正在使用Pex & Moles。我为我的 EF 对象上下文创建了痣和存根。
我想编写参数化的单元测试(我知道我首先编写了我的生产代码,但我必须这样做,因为我正在测试 Pex 和 Moles)来测试此方法是否返回有效的项目列表。
这是我的测试课:
[PexClass]
public class RepoTest
{
[PexMethod]
public void GetItemsTest(ObjectSet<DBItem> items)
{
MEFContext.ConstructorString = (@this, name) => {
var mole = new SEFContext();
};
DBRepository repo = new DBRepository();
IList<BLItem> result = repo.GetItems();
IList<DBItem> manual = items.Where(i => i.Changed > DateTime.Today.AddDays(-10));
if (result.Count != manual.Count)
{
throw new Exception();
}
}
}
然后我为这个特定的参数化单元测试运行Pex Explorations,但我得到一个错误path bounds exceeded。Pex 通过提供null
给这个测试方法 (so items = null
) 来开始这个测试。这是 Pex 正在运行的代码:
[Test]
[PexGeneratedBy(typeof(RepoTest))]
[Ignore("the test state was: path bounds exceeded")]
public void DBRepository_GetTasks22301()
{
this.GetItemsTest((ObjectSet<DBItem>)null);
}
这是 Pex 提供的附加评论:
对于这些输入,测试用例运行时间过长,Pex 停止了分析。请注意:方法 Oblivious.Data.Test.Repositories.TaskRepositoryTest.b__0 被调用了 50 次;请检查代码是否陷入无限循环或递归。否则,单击“设置 MaxStack=200”,然后再次运行 Pex。
更新属性 [PexMethod(MaxStack = 200)]
问题
我这样做是否正确?我应该改用 EFContext 存根吗?我是否必须在测试方法中添加其他属性,以便 Moles 主机运行(我不确定现在是否可以)。我只运行 Pex & Moles。没有 VS 测试或 nUnit 或其他任何东西。
我想我应该对 Pex 设置一些限制,它应该为这个特定的测试方法提供多少项目。