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
is there a workaround in mocking this one its seal with internal constractor
asked by tolisss (28.8k points)

7 Answers

0 votes
Sorry,
can you repeat the question?
answered by lior (13.2k points)
0 votes
sure
what i want is to mock System.Web.HttpPostedFile that class has an internal constractor and its also sealed

could u please point a workaround on this
answered by tolisss (28.8k points)
0 votes
nobody?
answered by tolisss (28.8k points)
0 votes
Hi tolisss

Sorry for not responding earlier.

I you use Mock like this (reflective):

Mock<HttpPostedFile> mock = MockManager.Mock<HttpPostedFile>(Constructor.NotMocked);
mock.ExpectGet("FileName","MyFile");


You can set expectation on the instance that will be created later. If you post a code example here I can give a better solution. I wonder what you try to accomplish, and if this is the correct way to achieve it.

Please post an example and define what you want to test.
answered by gilz (14.5k points)
0 votes
Hi gliz

1st i want to complain cause nobody fixed my notification problem yet

2nd) look at the method bellow
public static void GetFileMediaValue(FileField fileField,EcmsLogOnContext ecmsLogOnContext, EcmsListItem listItem)
        {

            HttpPostedFile postedFile = fileField.PostedFile;
            

            if (postedFile != null && postedFile.ContentLength > 0)
            {
                int fileLength = postedFile.ContentLength;
                var mem = new byte[fileLength];
                Stream postedStream = postedFile.InputStream;
                postedStream.Read(mem, 0, fileLength);


                EcmsFileManager.GetAnInstance(ecmsLogOnContext).AssignFile(listItem, (EcmsFieldFile) fileField.Field,
                                                                           mem, postedFile.ContentType, fileField.FileName);

            }
        }

and here is th test method
        [Test]
        public void GetFileMediaValue()
        {
            EcmsListItemMockHelper.ConstractorMock();
            var ecmsListItem = new EcmsListItem(Guid.Empty, Guid.Empty, 0, null);
            var context = new EcmsLogOnContext();
            var ecmsFieldFile = new EcmsFieldFile();
            var fileField=new FileField(ecmsFieldFile);
            fileField.PostedFile=new HttpPostedFile();
            using (RecordExpectations expectations = RecorderManager.StartRecording())
            {

                EcmsFileManager.GetAnInstance(context).AssignFile(ecmsListItem, ecmsFieldFile, null,
                                                                  fileField.PostedFile.ContentType, fileField.FileName);
                expectations.IgnoreArguments();
            }

            BaseFieldControlExtensions.GetFileMediaValue(fileField, context, ecmsListItem);
        }


but the line
fileField.PostedFile=new HttpPostedFile();


cannot be compiled of course
answered by tolisss (28.8k points)
0 votes
Hi Tolisss,

First I would like to apologize about the notifications. We thought we had it fixed - we'll investigate again.

From your example, I see you want to return the object, and there are two ways to do that. The first is what I described but that means you will need to start before your function.

The second one is to use MockObject. It has a way to construct an object, which is not easy to construct, and pass it parameters. The problem is that one of the parameters is of an internal type. You would need its type for that. And since it is an internal type, you'll need to use some reflection.

So it's not easy, but it is possible.

Let me know what you think.
answered by gilz (14.5k points)
0 votes
Hi Tolisss,

First I would like to apologize about the notifications. We thought we had it fixed - we'll investigate again.


Hi gilz

i though u had fix it also cause i receive a notification from another post i have made but this do not work for all my post please do some kind of reset to my account and send some test mails

as for your mockobject suggestion works perfectly although i did not understand how to implement your first suggestion
answered by tolisss (28.8k points)
...