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
I'm trying to figure out how to typemock a generic method.

The line of code that I am trying to mock looks as follows:

foreach (X<Y> x1 in inst.Translate<X<Y>>())

where Translate is defined as IEnumerable <T> Translate<T>() where T : Element<D>;

My test is as follows:

UnitTestSupport.ForEachSupport<X<Y>> mockForEachLoop2
= new UnitTestSupport.ForEachSupport<X<Y>>();

MockObject mockRetX = MockManager.MockObject(typeof(X<Y>));

mockForEachLoop2.LoopStart("Translate");

where LoopStart is:
namespace UnitTestSupport
{
public class ForEachSupport<TContainer>
{
mockIEnumerable = MockManager.MockObject(typeof(IEnumerable<TContainer>));

public void LoopStart(string firstCall)
{
mockTContainer.ExpectAndReturn(firstCall, mockIEnumerable.Object as IEnumberable<TContainer>);
mockIEnumerable.ExpectAndReturn("GetEnumerator", mockIEnumerator.Object as IEnumerator<TContainer>);
} // Start
}
}

I'm not sure how to define the generic type of Translate. When I run it this way I get errors.

Test method HandlerTest threw exception: TypeMock.TypeMockException:
*** No method Translate in type X`1[Y] returns IEnumerable`1[X`1[Y].

Thanks for the help.
asked by darkelf1217 (1.3k points)

9 Answers

0 votes
Hi
Sorry :oops: I could not reproduce your problem.
Can you post the mock statements for mockTContainer and mockIEnumerator ?
answered by ohad (35.4k points)
0 votes
Here are the statements that you requested:

private Mock mockTContainer;
mockTContainer = MockManager.MockObject(typeof(TContainer));

private MockObject mockIEnumerator;
mockIEnumerator = MockManager.MockObject(typeof(IEnumerator<TContainer>));

Thanks.
answered by darkelf1217 (1.3k points)
0 votes
Hmm
Still I can nor reproduce the error.
Can you tell me where Translate is defined?
Is it in class X?
answered by ohad (35.4k points)
0 votes
Translate is inherited. It comes from:

public class X<D> : Element<D>

where

public abstract class Element<D>
{
public virtual IEnumerable<T> Translate<T>() where T : Element<D>;
}

Thanks.
answered by darkelf1217 (1.3k points)
0 votes
Hi
Sorry still I can not re-produce your problem.
It seems to me like you have a bug in your test.
Can you try the write the same test hard coded?
That is - skip the ForEachSupport class.
Maybe if we will look at the code in its simplify version we could identify the problem. :shock:
answered by ohad (35.4k points)
0 votes
Ok. I've solved my problem. I was missing the typeof() statement for my generic function. After that I was having another issue which was solved by upgrading to 3.71 from 3.6.

Thanks.
answered by darkelf1217 (1.3k points)
0 votes
Great that you found the solution
Could you post it so that other developers could see it?
answered by scott (32k points)
0 votes
The function LoopStart(string firstCall) should look like below:

public void LoopStart(string firstCall)
{
mockTContainer.ExpectAndReturn(firstCall, mockIEnumerable.Object as IEnumerable<TContainer>,typeof(TContainer));
mockIEnumerable.ExpectAndReturn("GetEnumerator", mockIEnumerator.Object as IEnumerator<TContainer>);
} // Start

I was missing the typeof(TContainer)); in the ExpectAndReturn call.
answered by darkelf1217 (1.3k points)
0 votes
Thanks for sharing with us the info 8)
answered by ohad (35.4k points)
...