chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi All,
Below given is the method i need to invoke the test

AgentDataAccess.GetAgents(instanceId) call returns a Idatareader, while i execute the test it gave an error
TypeMock.VerifyException:
TypeMock Verification: Unexpected Call to System.Data.IDataReader.Dispose().


public static List<Agent> GetAgents(int instanceId)
        {
            return Agent.CreateAgents(AgentDataAccess.GetAgents(instanceId));
        }



[testMethod]
public void TestMethod()
{
            Mock mockAgentDataAccess = MockManager.Mock(typeof(mockAgentDataAccess));
            MockObject mockIDataReader = MockManager.MockObject(typeof(IDataReader));
            mockAgentDataAccess.ExpectAndReturn("GetAgents", mockIDataReader.Object as IDataReader);
  mockIDataReader.ExpectAndReturn("Read",true); 

 }               
          
            List<Agent> results = AgentController.GetAgents(1);
               
            MockManager.Verify();



Can you let me know why i am getting this error

i have tried with mockIDataReader.ExpectCall(dispose());

The dispose verification exception went away , but it gave me another verification exception getOrdinal()


It looks like it is going a chain way, What am i missing here

thanks

thomson
asked by tomexplore (1.8k points)

5 Answers

0 votes
Hi,


I think there are a few issues here, that may cause your symptoms. So let's start at the top.

Mock mockAgentDataAccess = MockManager.Mock(typeof(mockAgentDataAccess));


In this line of code you should send the type of AgentDataAccess, not mockAgentDataAccess (which is of course of type Mock).

I'm assuming you left out the definition of DashboardDataAccess since it is not relevant. But you do set the expectation for GetAgents on it. Is this your intent?

Next, on the mockIDataReader you set the expectation for Read. But I don't see where you call this method.

So here's what I suggest: Make the corrections above, and make sure you set the expectation for the correct types. Once you have this set up, post the complete code (test and code) and we can continue, if things still do not work.

Let me know how it went.
answered by gilz (14.5k points)
0 votes
Mock mockAgentDataAccess = MockManager.Mock(typeof(AgentDataAccess));


I have changed the code and the expectations

are

mockDashboardDataAccess.ExpectAndReturn("GetAgents", mockIDataReader.Object as IDataReader);


My Expectation it should return an Idatareader [which inturn should return a Agents]

I need to see whether the above call is mocked and check the returning results
answered by tomexplore (1.8k points)
0 votes
Hi Thomson,

Looking more closely, it may be simpler than what I thought. From what I understand, you are trying to mock the GetAgents method and return a fake value.

I'm assuming mockDashboardDataAceess is a mock of type AgentDataAccess. Now, the type you return from GetAgents is a List<Agent>, not a IDataReader.

The simplest way to do this is to write something like this:

List<Agent> fakeAgents = new List<Agent>
/// Fill fakeAgents

mockDashboardDataAccess.ExpectAndReturn("GetAgents", fakeAgents);


Now when you call GetAgents, it will return a List<Agent> with the value you supply.

Try it out. And if I'm wrong in my understanding let me know. If it's not working, let me know.
answered by gilz (14.5k points)
0 votes
Hi ,
Since it return an IDatareader i cannot give a fake list, it gives me an error , the type expecting is IdataReader
answered by tomexplore (1.8k points)
0 votes
Hi Tom,

Basically the error that you are getting means that during the execution of the code the mocking that you are doing is working but for some reason (which cant be seen in the pieces of code you gave) someone is "disposing" with the mocked IDataReader that you have created.
if that is how it should be you just need to add another expectation to the dispose call.

However Gil was right according to the code you gave the GetAgents methods return type is List<Agent>
so I'm guessing that some of the code is not seen here.
Specifically in order to help you please let us know the following:
1. The return type of AgentController.GetAgents
2. what is mockDashboardDataAccess? what type is it how did you create it...
3. the relation between AgentDataAccess, mockDashboardDataAccess and AgentController (who calls who and such)

If you are hesitant on posting large parts of you r code here let us know and we can take it offline.
answered by lior (13.2k points)
...