I am using Moq framework for Mocking.
I have a SocialWorker which derives from an abstract DataWorker class.
The SocialWorker has reference to couple of Repositories as a virtual property
I am trying to run the following test:
private Place _place; private Mock<IRepository<Resources.Data.Place>> _placeRepositoryMock; private Mock<SocialWorker> _socialWorkerMock; [SetUp] public void SetUp() { _place = new Place {Name = "A"}; _socialWorkerMock = new Mock<SocialWorker> {DefaultValue = DefaultValue.Mock}; IRepository<Resources.Data.Place> placeRepository = _socialWorkerMock.Object.PlaceRepository; _placeRepositoryMock = Mock.Get(placeRepository); _placeRepositoryMock.Setup( repository => repository.Find(It.IsAny<Expression<Func<Resources.Data.Place, bool>>>(), null, null)) .Returns(new[] {new Resources.Data.Place()}); } [Test] public void AddShouldAddANewPlace() { var placeManager = new PlaceManager(_socialWorkerMock.Object); object placeEntity = placeManager.Add(_place); placeEntity.GetType().Should().Equal(typeof (Resources.Data.Place)); _socialWorkerMock.Verify( socialWorker => socialWorker.PlaceRepository.Add(It.IsAny<Resources.Data.Place>()), Times.Once()); _placeRepositoryMock.Verify( placeRepository => placeRepository.Find(p => p.Name.Equals(_place.Name), null, null).First(), Times.Once()); } The last verification of this test fails with this error:
System.NotSupportedException : Invalid verify on a non-virtual (overridable in VB) member: placeRepository => placeRepository.Find(p => p.Name.Equals(._place.Name), null, null).First<Place>() I am fairly new to Moq and unit testing in general.
Here is the relevant code for your reference:
IUnitOfWork
public interface IUnitOfWork : IDisposable { void CommitChanges(); } IRepository
public interface IRepository<T> { void Add(T entity); void Delete(T entity); IEnumerable<T> Find(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, IList<string> includedProperties = null); T FindById(object id); void Update(T entity); } DataWorker
public abstract class DataWorker : IUnitOfWork { protected ObjectContext ObjectContext; private bool _disposed; protected DataWorker() { ObjectContext = new ObjectContext(ConfigurationManager.ConnectionStrings["DataEntities"].ConnectionString); } ~DataWorker() { . . . } protected virtual void Dispose(bool disposing) { . . . } public virtual void CommitChanges() { ObjectContext.SaveChanges(); } public void Dispose() { . . . } } DataRepository
public class DataRepository<T> : IRepository<T> where T : EntityObject { private readonly ObjectSet<T> _objectSet; public DataRepository(ObjectContext objectContext) { _objectSet = objectContext.CreateObjectSet<T>(); } public void Add(T entity) { . . . } public void Delete(T entity) { . . . } public virtual IEnumerable<T> Find(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, IList<string> includedProperties = null) { . . . } public T FindById(object id) { . . . } public void Update(T entity) { . . . } } SocialWorker
public class SocialWorker : DataWorker { private IRepository<ContactRequest> _contactRequestRepository; private IRepository<Place> _placeRepository; private IRepository<User> _userRepository; public virtual IRepository<ContactRequest> ContactRequestRepository { get { return _contactRequestRepository ?? (_contactRequestRepository = new DataRepository<ContactRequest>(ObjectContext)); } } public virtual IRepository<Place> PlaceRepository { get { return _placeRepository ?? (_placeRepository = new DataRepository<Place>(ObjectContext)); } } public virtual IRepository<User> UserRepository { get { return _userRepository ?? (_userRepository = new DataRepository<User>(ObjectContext)); } } }