-
-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help required in mocking generic repository #576
Comments
Hi @unittester57, this issue tracker is used mostly for bug reports and for discussing & planning changes and enhancements. Our resources for answering pure usage questions are very limited. I suggest you ask over at Stack Overflow. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have implemented generic repository in my project. Now I am writing test cases for my consumer. I am trying to mock database function through Moq but I am getting values from database rather than the one I faked through Moq. Below I am sharing my implementation. Kindly help me in resolving this issue:
My interface:
public interface IEventsRepository<T> : IRepository<T> { T GetEventsByEventId(int eventId); }
My class:
public class EventsTableRepository : EFDBRepository<EventsModel>, IEventsRepository<EventsModel> { public EventsModel GetEventsByEventId(int eventId) { return _dbSet.Where(x => x.EventID == eventId).FirstOrDefault();}}
My Consumer:
public static Response<string> EventsAccept(EventsAlertsRequest logMsgId) { IEventsRepository<EventsModel> eventsRepo = (IEventsRepository<EventsModel>)RepositoryLocator.GetRepositoryObject(STMEnums.RepositoryName.EventsTableRepository.ToString()); EventsModel eventmodel = new EventsModel(); eventmodel = eventsRepo.GetEventsByEventId(eachlogMsgId);}
Test Method:
[TestMethod()] public void EventsAcceptSuccessTest() { EventsModel eventmodel = new EventsModel(); eventmodel.Message = "TEST"; Mock<IEventsRepository<EventsModel>> obj = new Mock<IEventsRepository<EventsModel>>(); obj.Setup(m => m.GetEventsByEventId(Moq.It.IsAny<int>())).Returns(eventmodel); EventStatusChangeResponse = Diagnostics_.EventsAccept(logMsgId); Assert.AreEqual(eventmodel.Status, EventStatus.ACCEPTED);
The text was updated successfully, but these errors were encountered: