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
asked by eli (5.1k points)

4 Answers

0 votes

I have this test setup:

var targetLabel = new Label { ID = "label" };

var p = Isolate.Fake.Instance<Panel>();
Isolate.WhenCalled(() => p.ID).WillReturn("panel");
p.Controls.Add(new TextBox { ID = "text" });

var p2 = Isolate.Fake.Instance<Panel>();
Isolate.WhenCalled(() => p2.ID).WillReturn("panel2");
p2.Controls.Add(new Label { ID = "label2" });
p2.Controls.Add(new TextBox { ID = "text2" });

Isolate.WhenCalled(() => p2.FindControl("label")).WithExactArguments().WillReturn(null);
Isolate.WhenCalled(() => p.FindControl("label")).WithExactArguments().WillReturn(targetLabel);

This is for a processing component that loops through the structure. As it loops through the structure, I want, when it gets to the p2 element, to return null, which will continue the looping, but when p is hit, it will return a match, which gets returned to the caller.

The issue is, FindControl returns a control, so it doesn't actually return a targetLabel; the assert;


Fails because the FindControl() method returns a faked control instance,not my targetLabel control reference.... so how can I mock FindControl, because the issue is its referring to all the controls as Control, and not their respective types.

answered by bmains (13.2k points)
0 votes

Basically, you can use WillReturn() to return a derived type and the assertion should work. What's throwing me off in your code is this usage:
var p = Isolate.Fake.Instance<Panel>();

Considering that p is a recursive fake, any calls originating from it are fake as well, meaning p.Controls.Add() doesn't do anything. Is this necessary for what you're trying to test?

Typemock Support
answered by doron (16.5k points)
0 votes
answered by scott (32k points)
0 votes

True, good point :-) I had actual objects at first, then switched them to fakes for some reason... wasn't thinking I guess. Anyway, originally I was getting errors but now it seems to work. Awesome.

answered by bmains (13.2k points)