Skip to content
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

Closed
NaveenRathan opened this issue Jan 18, 2018 · 1 comment
Closed

Help required in mocking generic repository #576

NaveenRathan opened this issue Jan 18, 2018 · 1 comment

Comments

@NaveenRathan
Copy link

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

@stakx
Copy link
Contributor

stakx commented Jan 18, 2018

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.

@stakx stakx closed this as completed Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants