I've managed to Mock an Entity Framework dbcontext and dbset to allow for unit testing querying functions against a repository component.
I've been unable to perform a successful test against an a method using Entity Frameworks' AddOrUpdate() method. The error received is:
"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
in your code. – Gert ArnoldAddOrUpdate
being an extension method. – Gert Arnold