我已經設法模擬一個實體框架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
}
};
}
我在代碼中看不到'AddOrUpdate'。 –
但它可能與'AddOrUpdate'是一個擴展方法有關。 –
AddOrUpdate在具體的JobListingRepository中使用。如何使用擴展方法執行測試? – ManxJason