Fixes for problems with SubSonic 3's TestRepository

我的梦境 提交于 2019-12-11 00:41:25

问题


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;
    }

回答1:


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:


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



来源:https://stackoverflow.com/questions/2454889/fixes-for-problems-with-subsonic-3s-testrepository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!