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
It seems that the recursive mocking ability is only available with recursive fakes, is it possible to make it available even though a member is set to callOriginal?


Here's what I want to do:
 controller = Isolate.Fake.Instance<HomeController>(Members.CallOriginal);
            HttpPostedFileBase hpfb = PostFileFactory();
Isolate.Swap.CallsOn(controller.Request.Files).WithCallsTo(new List<HttpPostedFileBase> { hpfb });


All of the properties of controller are real, except for Request because Request.Files is fake.
________
Yamaha RT100
asked by nsoonhui (59.1k points)

6 Answers

0 votes
My current workaround is to have a fake request object as the buffer, such as this:
        controller = Isolate.Fake.Instance<HomeController>(Members.CallOriginal);

            HttpPostedFileBase hpfb = PostFileFactory();
            HttpRequestBase requestFake = Isolate.Fake.Instance<HttpRequestBase>(Members.ReturnRecursiveFakes);

            Isolate.Swap.CallsOn(requestFake.Files).WithCallsTo(new List<HttpPostedFileBase> { hpfb });
            Isolate.WhenCalled(() => controller.Request).WillReturn(requestFake);


But as you can see, if we can do away with the requestFake just just set the controller.Request.Files to a duck typing swap, that would be great!
________
aromed vaporizers
answered by nsoonhui (59.1k points)
0 votes
Hi Soon

Try using WillReturnCollectionValuesOf as suggested in your previous post

This will fake the whole chain controller.Request.Files
while using Swap.CallsOn calls the real methods and swaps the returned object from the chain.

Note that your solution will have the same results but with slightly more code.
answered by ohad (35.4k points)
0 votes
Thanks ohad,

but it seems that even with WillReturnCollectionValuesOf I still have to create a dummy object to store the Request object if I use Membership.CallOriginal in contructor. Isn't it?
________
JR80
answered by nsoonhui (59.1k points)
0 votes
Hi Soon

Actually no :)
When you specify a chain inside WhenCalled method all the chain will be faked even if you use Members.CallOriginal on the controller instance.

The Members.CallOriginal argument is used as a default when methods and properties of the faked class are called and you didn't specified explicitly their behavior.
answered by ohad (35.4k points)
0 votes
Hi Soon

Actually no :)
When you specify a chain inside WhenCalled method all the chain will be faked even if you use Members.CallOriginal on the controller instance.

The Members.CallOriginal argument is used as a default when methods and properties of the faked class are called and you didn't specified explicitly their behavior.


I see, I think I might have discover another bug ( or limitation?)

If I use CallsOn this ability seems to be lost. Here's my production code:
    public class HoldHttpFileColllection
    {
        public HttpFileCollectionBase Files
        {
            get;
            private set;

        }
    }

    public class HHtpWrapper
    {
        public HoldHttpFileColllection filesCollection
        {
            get;
            private set;
        }
    }


And here's the test code:
        [Test, Isolated]
        public void Test2()
        {

            HHtpWrapper myWrap = Isolate.Fake.Instance<HHtpWrapper>(Members.CallOriginal);
            Isolate.Swap.CallsOn(myWrap.filesCollection.Files).WithCallsTo(new string[] { "hello" });
        }


It seems that the CallsOn cannot take recursive faking; if I run the above code I will get a nullreferenceexception:
________
vaporizer wholesale
answered by nsoonhui (59.1k points)
0 votes
Hi Soon

Sorry, I think I didn't explained myself well :oops:

The difference between Swap.CallsOn method and WillReturnCollectionValuesOf is that the first is taking an Object as an argument, this means that calls inside Swap.CallsOn are evaluated without faking.
WillReturnCollectionValuesOf on the other hand takes lambda expression which can be faked.

This should work:
HHtpWrapper myWrap = Isolate.Fake.Instance<HHtpWrapper>(Members.CallOriginal);
Isolate.WhenCalled(() => myWrap.filesCollection.Files).WillReturnCollectionValuesOf(new string[] { "hello" });
answered by ohad (35.4k points)
...