本文介绍了结合使用带有实体框架6存储库的AddOrUpdate的单元测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我设法模拟了实体框架dbcontext和dbset,以允许针对存储库组件进行单元测试查询功能。

I've managed to Mock an Entity Framework dbcontext and dbset to allow for unit testing querying functions against a repository component.

我无法针对使用Entity Frameworks的AddOrUpdate()方法的方法执行成功的测试。收到的错误是:

I've been unable to perform a successful test against an a method using Entity Frameworks' AddOrUpdate() method. The error received is:

无法在派生的IDbSet类型'Castle.Proxies.DbSet`1Proxy'上调用公共实例方法AddOrUpdate。找不到方法。

"Unable to call public, instance method AddOrUpdate on derived IDbSet type 'Castle.Proxies.DbSet`1Proxy'. Method not found."

是否有可能对此进行测试?

Is it at all possible to test this?

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


推荐答案

问题是因为AddOrUpdate是扩展方法。我通过设计包装器解决了这个问题。您可以模拟/存根IAddOrUpdateHelper接口。

The issue is because AddOrUpdate is an extension method. I overcame this issue by designing a wrapper. You can mock/stub the IAddOrUpdateHelper interface instead.

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

这篇关于结合使用带有实体框架6存储库的AddOrUpdate的单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:50