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, i'm quite new in TypeMock. I wonder if it is possible to record in TypeMock ordered and unordered sequences of calls. Something similar to ordered/unordered statment in NMock or RhinMock. Or maybe there is another solution to control data flow from one object to an other. I have some pice of code like this:

ArrayList al = new ArrayList() ;
a.add( algorythm1 ) ;
a.add( algorythm1 ) ;
a.add( algorythm1 ) ;

...

foreach( Algorithm a in al )
{
   date d = a.invoke() ;
   decorator1( d ) ;
   decorator2( d ) ; 
   presenter.parse( dummy ) ;
}


And i want to use mock for object from list al and for presenter. So how can i check correctness of data passing to presenter?
Thanks for help
asked by Lukasz Czekaj (1.2k points)

5 Answers

0 votes
Lukasz,
We currently don't support ordered mocks, as this feature was never required (until now) by our customers.

I am trying to understand the code example. What are you trying to test (what scenario)?
answered by scott (32k points)
0 votes
ok, may be this will be more clear

foreach (IDataCrawler md in dataCrawlers)
{
       int id = md.id;
       data gL = md.getData();

       //big part of code which process gL
       //and change state of gL
       //i want to test this big part of code 

       reportNotes.AddToReport(id, gL, time);
}


I can control by Mocks elements from dataCrawlers, i want to check behavior of "big part of code" in case of large set of dataCrawlers so my idea is to check data flow from md to reportNotes. Maybe it is not fitted too much to idea of unit tests and mocks and it much more coresponds to testing by debugger but as i said i'm new in mocking.
answered by Lukasz Czekaj (1.2k points)
0 votes
Ok,
This could have quite a few implementations here is one:

// create fake data crawlers
mockDataCrawlers.Add(element1);
mockDataCrawlers.Add(element2);

using (RecordExpectations rec = RecorderManager.StartRecording())
{
   // mock reportNotes (is created in method)
  ReportNotes mockedReport = new ReportNotes();

  // mock the elements
  rec.ExpectAndReturn(element1.id,1);
  data dataOfElement1 = element1.getData(); // keep mocked data
  rec.Return(...);

  rec.ExpectAndReturn(element1.id,2);
  data dataOfElement2 = element2.getData(); // keep mocked data
  rec.Return(...);

  // make sure that the reports is called correctly
  mockedReport.AddToReport(1, dataOfElement1, time1); 
  rec.CheckArguments();
  
  mockedReport.AddToReport(2, dataOfElement2, time2); 
  rec.CheckArguments();
}

:arrow: Notice the CheckArguments() this will tell TypeMock to fail if the arguments passed are not the same as expected and we expect AddToReport to be called the first time with 1, element1 and time1. Because element1 is mocked, TypeMock will check that the mock was passed!

:idea: There might be a case where you won't want to validate the time argument. We can then use:

  mockedReport.AddToReport(0, null, null); 
  rec.CheckArguments(1,Check.IsMock(dataOfElement1), Check.IsAny());
  
  mockedReport.AddToReport(0, null, null); 
  rec.CheckArguments(2,Check.IsMock(dataOfElement2), Check.IsAny());

:arrow: Notice that we now pass the arguments to the CheckArguments method, this give us more control on the argument checks.
See Check.IsMock, this will verify that the argument passed is the actual mock.
answered by scott (32k points)
0 votes
Ok, but if ther is not correlation one to one betwenen gL of algorithm input and algorithm output but many to one? I mean if we have following sequence of algorithm input and outputs: {(1,1),(2,1),(3,2),(4,2)} we cant ditinguish that sequences from {(1,1),(2,2),(3,1),(4,2)}. If we can expect ordered sequences of call it will be possible to distinguish this two cases.
answered by Lukasz Czekaj (1.2k points)
0 votes
I will explain:
Each method call is sequenced.
That means that if we expect Method to be called as follows:
using (RecordExpectations rec = RecorderManager.StartRecording())
{ 
   rec.DefaultBehavior.CheckArguments();
   Method(1);
   Method(2);
   Method(3);
}

Then TypeMock will expect the calls to be called in that order.
Only sequences between different methods are not checked.
using (RecordExpectations rec = RecorderManager.StartRecording())
{ 
   rec.DefaultBehavior.CheckArguments();
   Method(1);
   Foo(2);
}

Both these will pass
   Method(1);
   Foo(2);

//-----------and
   Foo(2);
   Method(1);
answered by scott (32k points)
...