Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何测试使用linq2sql和存储库模式实现的DAL?_Unit Testing_Linq To Sql_Repository - Fatal编程技术网

Unit testing 如何测试使用linq2sql和存储库模式实现的DAL?

Unit testing 如何测试使用linq2sql和存储库模式实现的DAL?,unit-testing,linq-to-sql,repository,Unit Testing,Linq To Sql,Repository,这是我生活的一部分 DataContext接口: public interface IDataContextFactory { System.Data.Linq.DataContext Context { get; } void SaveAll(); } public interface IRepository<T> where T : class { /// <summary> /// Return all instances of t

这是我生活的一部分

DataContext接口:

public interface IDataContextFactory
{
    System.Data.Linq.DataContext Context { get; }
    void SaveAll();
}
public interface IRepository<T> where T : class
{
    /// <summary>
    /// Return all instances of type T.
    /// </summary>
    /// <returns></returns>
    IEnumerable<T> GetAll();
}
以下是我的数据上下文类,其中包含生成的类:

partial class InternetRailwayTicketSalesDataContext: DataContext, IDataContextFactory
{
    public System.Data.Linq.DataContext Context
    {
        get { return this; }
    }

    public void SaveAll()
    {
        this.SubmitChanges();
    } 
}
这是我的存储库界面:

public interface IDataContextFactory
{
    System.Data.Linq.DataContext Context { get; }
    void SaveAll();
}
public interface IRepository<T> where T : class
{
    /// <summary>
    /// Return all instances of type T.
    /// </summary>
    /// <returns></returns>
    IEnumerable<T> GetAll();
}
最后,这里是一个具体的存储库类实现:

public class Repository<T> : IRepository<T> where T : class
{
    protected IDataContextFactory _dataContextFactory;

    /// <summary>
    /// Return all instances of type T.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<T> GetAll()
    {
        return GetTable;
    }

    private System.Data.Linq.Table<T> GetTable
    {
        get { return _dataContextFactory.Context.GetTable<T>(); }
    }
}
class PasswayRepository:Repository<Passway>, IPasswayRepository
{
    public PasswayRepository(IDataContextFactory context)
        : base(context)    
    {                    
    }

    public bool IsPasswayExists(int id)
    {
        if (GetAll().Where(pass => pass.Id == id && pass.IsDeleted == false).Count() > 0)
            return true;
        else
            return false;           
    }   
}
class PasswayRepository:Repository,IPasswayRepository
{
公共PasswayRepository(IDataContextFactory上下文)
:基本(上下文)
{                    
}
公共bool IsPasswayExists(int-id)
{
if(GetAll().Where(pass=>pass.Id==Id&&pass.IsDeleted==false)。Count()>0)
返回true;
其他的
返回false;
}   
}

你能给我一个如何测试IsPasswayExists方法的例子吗?(如果愿意,您可以使用任何模拟框架)。

测试什么?你说的测试dal是什么意思?测试该方法的代码将创建一个“Passway”,检查其id,然后使用该id和无效id调用该methid,并验证正确的返回值,然后删除添加的“Passway”。顺便说一句,您的IsPasswayExists可以编写得更简单:return GetAll().Any(pass=>pass.id==id&&!pass.IsDeleted);但是如何打破对数据库的依赖呢?嗯,这就是依赖注入的要点。关于这一点的教程远远超出了本文的回答范围。您可以在存储库中插入模拟来代替上下文。