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
System.Web.HttpFileCollection has NO constructors and the class is sealed. How do I mock this class?
        Dim anUploadedFilesValidator As IUploadedFilesValidator
        anUploadedFilesValidator = New UploadedFilesValidator
        anUploadedFilesValidator.MaximumFiles = 10

        TypeMock.MockManager.Init()
        Dim httpFileCollectionMock As TypeMock.Mock = TypeMock.MockManager.Mock(GetType(System.Web.HttpFileCollection), TypeMock.Constructor.NotMocked)

        httpFileCollectionMock.ExpectGet("Count", 9)
        Assert.That(anUploadedFilesValidator.IsValid(CType(httpFileCollectionMock.MockedInstance, System.Web.HttpFileCollection)), [Is].True)


Here is my first attempt to do so. However I cannot call IsValid, since httpFileCollectionMock.MockedInstance returns Nothing.
asked by diangy (2.4k points)

5 Answers

0 votes
:arrow: When you use MockManager.Mock you are actually telling TypeMock to wait for the next time an HttpFileCollection is created, and when it is created it will be mocked. This is handy if an HttpFileCollection is created somewhere in the tested code.

:idea: To create a mocked instance of HttpFileCollection simply use:
MockManager.MockObject()
you can then use the Object property to get the actual object.

<Test(),VerifyMocks()> Public Sub Test()
        Dim anUploadedFilesValidator As IUploadedFilesValidator
        anUploadedFilesValidator = New UploadedFilesValidator
        anUploadedFilesValidator.MaximumFiles = 10

        Dim httpFileCollectionMock As TypeMock.MockObject (Of HttpFileCollection) = TypeMock.MockManager.MockObject( Of System.Web.HttpFileCollection))

        httpFileCollectionMock.ExpectGet("Count", 9)
        Assert.That(anUploadedFilesValidator.IsValid(httpFileCollectionMock.Object), [Is].True)
answered by scott (32k points)
0 votes
 
Dim aHttpFileCollection As System.Web.HttpFileCollection
        anUploadedFilesValidator = New UploadedFilesValidator
        TypeMock.MockManager.Init()
        Dim httpFileCollectionMock As TypeMock.MockObject(Of System.Web.HttpFileCollection) = TypeMock.MockManager.MockObject(Of System.Web.HttpFileCollection)()

        'When maximum files is set to 0, then any number of files is allowed
        anUploadedFilesValidator.MaximumFiles = 0

        httpFileCollectionMock.ExpectGetAlways("Count", 9)
        aHttpFileCollection = httpFileCollectionMock.Object
        Assert.That(anUploadedFilesValidator.IsValid(aHttpFileCollection), [Is].True)


I've changed the code to above. However within IsValid, aHttpFileCollection (the mocked object)'s count property always returns 0. Should it not return 9? Or am I doing something wrong?
answered by diangy (2.4k points)
0 votes
Hi
The Count property should return 9.
First you can check it directly in the test code like this:
'...
aHttpFileCollection = httpFileCollectionMock.Object
Assert.AreEqual(9, aHttpFileCollection.Count)
'...

Second you can use TypeMock Tracer to see whats going on with the mock object and the expectation.
(I suspect that another instance of HttpFileCollection is created inside IsVAlid)

If its possible please the post here the code of IsValid method and we'll try to see what's gone wrong.
answered by ohad (35.4k points)
0 votes
        TypeMock.MockManager.Init()
        Dim httpFileCollectionMock As TypeMock.MockObject(Of System.Web.HttpFileCollection) = TypeMock.MockManager.MockObject(Of System.Web.HttpFileCollection)()

        'When maximum files is set to 0, then any number of files is allowed
        anUploadedFilesValidator.MaximumFiles = 0

        httpFileCollectionMock.ExpectGetAlways("Count", 9)
        aHttpFileCollection = httpFileCollectionMock.Object
        Assert.AreEqual(9, aHttpFileCollection.Count)


In the above code I've done as suggested. However the assert.areequal fails, which means that the property Count is not being mocked?
answered by diangy (2.4k points)
0 votes
Hi,

This looks very wierd. :?
I suggest taking this offline.

I'll send you an email with instructions on how to prepare the log and trace file so we can further examine the issue.
answered by lior (13.2k points)
...