修复了 SubSonic 3 的 TestRepository 问题
我一直在尝试使用 SubSonic 3.0 的测试存储库支持进行单元测试,但遇到了一些问题,所以我想我记录了它们,以及我提出的修复:
无法使用
自动增量列显然 没有数据库,自动增量列不会自动工作,但如果像我一样,您对所有标识列使用简单的整数或长整型,则此修复效果很好:(
这是来自 此处,为了完整性而包含)
在 ActiveRecord.tt 中:
1:在函数顶部 public void Add(IDataProviderprovider){
public void Add(IDataProvider provider){
<#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#>
if (TestMode)
{
this.<#=tbl.PK.CleanName#>=++next_test_autoid;
}
<#}#>
2: 在 public bool TestMode = false 行下,添加:
public bool TestMode = false;
<#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#>
private static <#=tbl.PK.SysType#> next_test_autoid = 0;
<#}#>
对象相等比较被破坏
使用默认的 ActiveRecord 模板,对象相等不起作用。因此,从数据库中删除项目不起作用,因为 TestRepository 中使用的 List<>.Remove() 无法匹配要删除的项目。这可以通过以下方式在 tt 模板中修复:(即:将“==”替换为“Equals()”)
在 ActiveRecord.tt 中:
public override bool Equals(object obj){
if(obj.GetType()==typeof(<#=tbl.ClassName#>)){
<#=tbl.ClassName#> compare=(<#=tbl.ClassName#>)obj;
return compare.KeyValue().Equals(this.KeyValue());
}else{
return base.Equals(obj);
}
}
DeleteMany 未在测试存储库
操作中实现,如下所示 records.Delete( x => x.whatever ==whatever)
测试存储库失败,因为未实现 DeleteMany。解决这个问题需要获取源代码并自行构建,但这里有一个似乎有效的实现:
在 TestRepository.cs 中:
public int DeleteMany(Expression<Func<T, bool>> expression)
{
foreach (var x in _items.AsQueryable().Where(expression).ToList())
{
_items.Remove(x);
}
return 0;
}
I've been trying to use SubSonic 3.0's test repository support for unit testing but encountered a few issues, so I thought I document them, and the fixes I've come up with:
Auto-Increment Columns Don't Work
Obviously with no DB, auto-increment columns don't work automatically, but if like me you're using simple ints or longs for all identity columns, this fix works well:
(This is a copy from here, included for completeness)
In ActiveRecord.tt:
1: In the top of the function public void Add(IDataProvider provider){
public void Add(IDataProvider provider){
<#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#>
if (TestMode)
{
this.<#=tbl.PK.CleanName#>=++next_test_autoid;
}
<#}#>
2: Under the line public bool TestMode = false, add:
public bool TestMode = false;
<#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#>
private static <#=tbl.PK.SysType#> next_test_autoid = 0;
<#}#>
Object Equality Comparison is Broken
Using the default ActiveRecord template, object equality doesn't work. So removing items from the DB doesn't work since the List<>.Remove() used in the TestRepository fails to match the item being removed. This can be fixed in the tt templates with the following: (ie: replacing "==" with "Equals()")
In ActiveRecord.tt:
public override bool Equals(object obj){
if(obj.GetType()==typeof(<#=tbl.ClassName#>)){
<#=tbl.ClassName#> compare=(<#=tbl.ClassName#>)obj;
return compare.KeyValue().Equals(this.KeyValue());
}else{
return base.Equals(obj);
}
}
DeleteMany is Not Implemented in the Test Repository
Operations like this records.Delete(x => x.whatever == whatever)
fail against the test repo because DeleteMany is not implemented. Fixing this requires getting the source and building yourself, but here's an implementation that seems to work:
In TestRepository.cs:
public int DeleteMany(Expression<Func<T, bool>> expression)
{
foreach (var x in _items.AsQueryable().Where(expression).ToList())
{
_items.Remove(x);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您的帮助 - 但最好的办法就是告诉我们您的问题:)。 StackOverflow 更多的是用于回答问题 - 我可能建议前往 Github 并检查最新的源代码(我们已经修复了其中的一些)。如果您发现某些问题可以修复 - 非常欢迎使用补丁。
Thanks for this - but the best thing to do is to tell us about your issues :). StackOverflow is more for answering questions - I might suggest heading over to Github and checking the latest source (we've fixed a number of these). If you see that some things can be fixed - patches are very welcome.
至于第 2 点,如果记录尚未保存,这仍然会被破坏,因为它是在 KeyValue() 上进行比较的。为了确保未保存的记录也具有真正的相等性,我们还必须测试记录是否为New,如果是,则确定另一个相等策略
As to point 2, this is still broken if the record is not yet saved, as it is comparing on KeyValue(). To ensure that non-saved records also have true equality, we must also test if the record IsNew, and if so, determine another equality strategy