Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I've calls to my data access from another class.So while implementing moq I have :
C#
var mockdac = new Mock<idac>();
mockDac.SetUp(o => o.MyMethod(params)).Returns("something");

Inside MyMethod I have :
C#
var reader = Database.ExecuteReader(cmd);
while(reader.Read())
{
    //Some logic
}

My concern is to mock this ExecuteReader() and Read() methods.Any idea how this should go?

I tried mocking the IDataReader's Read() method but no help.

Regards,
Amit
Posted
Updated 21-Oct-14 2:09am
v2
Comments
Tomas Takac 21-Oct-14 15:49pm    
You are mocking the call to MyMethod() on IDAC to be able to test your "another class". That's correct. So why do you care how the method is actually implemented somewhere else? This has nothing to do with your test.
Member 11169453 22-Oct-14 5:46am    
Yes.That is how I tried. Instead I would like to mock the ExecuteReader().Thanks for your reply.Do you think it is possible?
Tomas Takac 22-Oct-14 7:10am    
So your intention is to test the data access class itself? Well you can mock data reader but looking at IDataReader interface it can be a lot of work. And you need to maintain the state across several calls to Read(). Maybe it's easier to write the stub yourself instead of using Moq.
Tomas Takac 23-Oct-14 5:02am    
Could you update your question with a sample how would you like to use it? i.e. assume you can create the mock, how would you use it in your unit tests? How would asserts look like?
Tomas Takac 23-Oct-14 9:09am    
I found this BTW:
http://stackoverflow.com/questions/2643909/how-to-mock-an-sqldatareader-using-moq-update

1 solution

For what's worth, I wrote this article based on your question: IDataReader Stub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900