2016-02-12 83 views
1

我已經設法模擬一個實體框架dbcontext和dbset,以允許單元測試查詢存儲庫組件的功能。單元測試方法使用AddOrUpdate實體框架6倉庫

我一直無法對使用實體框架的AddOrUpdate()方法的方法執行成功的測試。收到的錯誤是:

「無法調用公用,派生IDbSet類型實例方法AddOrUpdate'Castle.Proxies.DbSet`1Proxy'。未找到方法。」

是否有可能對此進行測試?

private IRepository _Sut; 
    private Mock<DbSet<JobListing>> _DbSet; 
    private Mock<RecruitmentDb> _DbContext; 

    [SetUp] 
    public void Setup() 
    { 
     _DbContext = new Mock<RecruitmentDb>(); 

     var JobsData = GenerateJobs().AsQueryable(); 

     _DbSet = new Mock<DbSet<JobListing>>(); 
     _DbSet.As<IQueryable<JobListing>>().Setup(x => x.Provider).Returns(JobsData.Provider); 
     _DbSet.As<IQueryable<JobListing>>().Setup(x => x.Expression).Returns(JobsData.Expression); 
     _DbSet.As<IQueryable<JobListing>>().Setup(x => x.ElementType).Returns(JobsData.ElementType); 
     _DbSet.As<IQueryable<JobListing>>().Setup(x => x.GetEnumerator()).Returns(JobsData.GetEnumerator()); 

     _DbContext.Setup(x => x.JobListings).Returns(_DbSet.Object); 
     _Sut = new JobListingRepository(_DbContext.Object); 
    } 

    [Test] 
    public void Update_ChangedTitleProperty_UpdatedDetails() 
    { 
     var Actual = GenerateJobs().First(); 
     var OriginalJob = Actual; 
     Actual.Title = "Newly Changed Title"; 
     _Sut.Update(Actual); 

     Actual.Title.Should().NotBe(OriginalJob.Title); 
     Actual.Id.Should().Be(OriginalJob.Id); 
    } 

    private List<JobListing> GenerateJobs() 
    { 
     return new List<JobListing> 
     { 
      new JobListing{ Id = 1, 
      Title = "Software Developer", 
      ShortDescription = "This is the short description", 
      FullDescription = "This is the long description", 
      Applicants = new List<Applicant>(), 
      ClosingDate = DateTime.Now.AddMonths(5).Date}, 

      new JobListing{ 
      Id = 2, 
      Title = "Head Chef", 
      ShortDescription = "This is the short description", 
      FullDescription = "This is the long description", 
      Applicants = new List<Applicant>(), 
      ClosingDate = DateTime.Now.AddMonths(2).Date 
      }, 

      new JobListing 
     { 
      Id = 3, 
      Title = "Chief Minister", 
      ShortDescription = "This is the short description", 
      FullDescription = "This is the long description", 
      Applicants = new List<Applicant>(), 
      ClosingDate = DateTime.Now.AddMonths(2).Date 
     } 
     }; 
    } 
+0

我在代碼中看不到'AddOrUpdate'。 –

+0

但它可能與'AddOrUpdate'是一個擴展方法有關。 –

+0

AddOrUpdate在具體的JobListingRepository中使用。如何使用擴展方法執行測試? – ManxJason

回答

1

問題是因爲AddOrUpdate是一種擴展方法。我通過設計一個包裝來克服這個問題。您可以模擬/存根IAddOrUpdateHelper接口。

public class AddOrUpdateHelper : IAddOrUpdateHelper 
    { 

     public void AddOrUpdateEntity<TEntity>(DataContext db, params TEntity[] entities) where TEntity : class 
     { 
      db.Set<TEntity>().AddOrUpdate(entities); 
     } 
    } 

    public interface IAddOrUpdateHelper 
    { 
     void AddOrUpdateEntity<TEntity>(DataContext db, params TEntity[] entities) where TEntity : class; 
    } 
+0

很好的答案,但是,你最有可能的意思是「DbContext」而不是「DataContext」。 – Ian

+0

複製和粘貼的危險 –

+0

@GregRTaylor我試圖按照這個建議,但是得到了異常:'無法調用public,派生IDbSet上的實例方法AddOrUpdate 'Castle.Proxies.DbSet1Proxy'類型。找不到方法。 – Kenz