Guest User

Mock DBSet<T>

a guest
Feb 18th, 2014
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="MockDbSet{T}.cs" company="Unit testing">
  3. //     Copyright (c) Unit testing. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6.  
  7. namespace UnitTests.Mocks
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Data.Entity;
  12.     using System.Linq;
  13.     using System.Text;
  14.  
  15.     public class MockDbSet<TEntity> : DbSet<TEntity>, IQueryable<TEntity> where TEntity : class
  16.     {
  17.         private List<TEntity> list = null;
  18.  
  19.         /// <summary>Initializes a new instance of the MockDbSet class.</summary>
  20.         public MockDbSet(IEnumerable<TEntity> collection)
  21.         {
  22.             this.list = new List<TEntity>(collection);
  23.         }
  24.  
  25.         public override IEnumerable<TEntity> AddRange(IEnumerable<TEntity> entities)
  26.         {
  27.             list.AddRange(entities);
  28.  
  29.             return list;
  30.         }
  31.  
  32.         public override TEntity Add(TEntity entity)
  33.         {
  34.             list.Add(entity);
  35.            
  36.             return entity;
  37.         }
  38.  
  39.         public override TEntity Attach(TEntity entity)
  40.         {
  41.             return entity;
  42.         }
  43.  
  44.         public new TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, TEntity
  45.         {
  46.             return (TDerivedEntity)list.FirstOrDefault();
  47.         }
  48.  
  49.         public override TEntity Create()
  50.         {
  51.             return list.FirstOrDefault();
  52.         }
  53.  
  54.         public override TEntity Find(params object[] keyValues)
  55.         {
  56.             return null;
  57.         }
  58.  
  59.         public override System.Collections.ObjectModel.ObservableCollection<TEntity> Local
  60.         {
  61.             get { return new System.Collections.ObjectModel.ObservableCollection<TEntity>(this.list); }
  62.         }
  63.  
  64.         public override TEntity Remove(TEntity entity)
  65.         {
  66.             list.Remove(entity);
  67.             return entity;
  68.         }
  69.  
  70.         public IEnumerator<TEntity> GetEnumerator()
  71.         {
  72.             return list.GetEnumerator();
  73.         }
  74.  
  75.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  76.         {
  77.             return list.GetEnumerator();
  78.         }
  79.  
  80.         public Type ElementType
  81.         {
  82.             get { return this.list.AsQueryable().ElementType; }
  83.         }
  84.  
  85.         public System.Linq.Expressions.Expression Expression
  86.         {
  87.             get { return this.list.AsQueryable().Expression; }
  88.         }
  89.  
  90.         public IQueryProvider Provider
  91.         {
  92.             get { return this.list.AsQueryable().Provider; }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment