使用Moq从Linq模拟DataContext到SQL

时间:2015-01-26 19:24:58

标签: c# unit-testing linq-to-sql moq

我正在编写一个单元测试来模拟数据上下文中的System.Data.Linq.Table。我的代码如下(请查看数据上下文,实体和单元测试)如下。代码没有编译说它无法解析方法。如何将此代码更改为格式良好的测试?

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name = "MyDB")]
public partial class Table1DataContext : System.Data.Linq.DataContext
{
    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
    partial void OnCreated();

    public Table1DataContext() :
        base(global::Properties.Settings.Default.DBConnectionString, mappingSource)
    {
        OnCreated();
    }

    public System.Data.Linq.Table<Table1Definition> Table1Definitions
    {
        get
        {
            return this.GetTable<Table1Definition>();
        }
    }
}

[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.Table1Definition")]
public partial class Table1Definition : INotifyPropertyChanging, INotifyPropertyChanged
{
    private int _ID;
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_ID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int ID
    {
        get
        {
            return this._ID;
        }
        set
        {
            if ((this._ID != value))
            {
                this.OnIDChanging(value);
                this.SendPropertyChanging();
                this._ID = value;
                this.SendPropertyChanged("ID");
                this.OnIDChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

[TestClass]
public class TestClass
{
    private Mock<Table1DataContext> dataContextMock;

    [TestMethod]
    public void ShoulReturnValidDefinition()
    {
        this.dataContextMock = new Mock<Table1DataContext>();
        this.dataContextMock.SetupGet(x => x.Table1Definitions).Returns(GetFakeEntityDefinition());

    }

    private static IList<Table1Definition> GetFakeEntityDefinition()
    {
        return new List<Table1Definition> { new Table1Definition { ID = 1 } };
    }
}

0 个答案:

没有答案