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
Hi there.

I'm very new to TypeMock and would like some help with the folllowing.

I have a test class with the following code:

Public Class BitsAndBobs

    Public Sub Testy()
        Dim tmpSite As SPSite = SPContext.Current.Site

        Console.WriteLine(tmpSite.Url)

        If tmpSite IsNot Nothing Then
            tmpSite.Dispose()
        End If

    End Sub
End Class


and in my MS test code I have the following:

<TestMethod()> _
    Public Sub MockTest2()

        Dim k As New SPSite("http://mycomputer:24000")

        Using recorder As New RecordExpectations

            Dim dummy As SPSite = SPContext.Current.Site

            recorder.Return(k)

        End Using

        Dim target As BitsAndBobs = New BitsAndBobs
   
        target.Testy()

        If k IsNot Nothing Then
            k.Dispose()
        End If
    End Sub


When I run the above test I get an error with the following:

Test method UnitTestTalks_Test.BitsAndBobsTest.MockTest2 threw exception: TypeMock.TypeMockException:
*** Cannot return a value for Main.Initialize() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly..


I guess I've got the syntax totally wrong! Could anyone tell me how to write the test properly? Basically, I'm would like to mock the SPContext.Current.Site property and pass my own custom SPSite object.

The idea is that I can test SharePoint webparts without having to install them first.

Thanks in advance.
Jas.
asked by MrClyfar (5.2k points)

8 Answers

0 votes
Hi.

I think I've worked it out. This code seems to work for me:

<TestMethod()> _
    Public Sub MockTest2()

        Dim mockSPSite As Mock = MockManager.MockObject(Of SPSite)(New String() {"http://mycomputer:24000"})


        Using recorder As New RecordExpectations
            Dim dumm As SPSite = SPContext.Current.Site

            recorder.Return(mockSPSite.MockedInstance)
        End Using

        Dim target As BitsAndBobs = New BitsAndBobs

        target.Testy()

        If mockSPSite.MockedInstance IsNot Nothing Then
            DirectCast(mockSPSite.MockedInstance, SPSite).Dispose()
        End If
    End Sub


Cool :)

Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi Jas
Actually the first example you gave should have worked so it seems like you found a bug.
Can you tell me please:
1. What version of Visual Studio you are using?
2. Did you run the test under debug when it happened?
answered by ohad (35.4k points)
0 votes
Hi there.

Appologies for the delay, been off work with a cold so I haven't been checking up on the forums.

I will find out tomorrow the exact Visual Studio version number, but I got the error whilst using Visual Studio 2008.

I had the error whilst running the unit test both in debug stepping mode (putting a breakpoint on the test and running line by line) and by just doing a 'Run Selection' on the test method and letting the unit test run normally.

Also, I found that just before I got the second example working, whilst I was experimenting with the code, I did get a lot of crashes where VSTestHost.exe had to be terminated. Once I got the test method syntax correct, the VSTestHost.exe errors vanished.

(I think I got the error because I used:

recorder.Return(mockSPSite)


rather then
recorder.Return(mockSPSite.MockedInstance)


so it's my fault rather then any TypeMock code.)

I did a Google search on this just now and found this:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=173802&SiteID=1

This might be helpful for other customers who have experienced crashes whilst running unit test using the MS Test envirnoment.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi Jas,

Thank you for the info (the exact version number is not important)

Some of the info you gave relates with defects that we have seen and are trying to solve. However after rereading I still have a nagging feeling that some of the things you have tried should have worked.

If you have the time and can reproduce one of the errors that caused the MSTest to crash, let me know and we will try to investigate the issue further.
answered by lior (13.2k points)
0 votes
Hi.

Ok now I'm a bit lost. I just tried this code, which I was getting errors with before:

Dim k As New SPSite("http://mycomputer:24000") 

        Using recorder As New RecordExpectations 

            Dim dummy As SPSite = SPContext.Current.Site 

            recorder.Return(k) 

        End Using 

        Dim target As BitsAndBobs = New BitsAndBobs 
    
        target.Testy() 

        If k IsNot Nothing Then 
            k.Dispose() 
        End If 


and now it works?!

Basically I wanted to try to reproduce the error I was getting before, but now the test method just works. I'll keep looking into this and try to get it to fail again. I hate it when that happens! :)

Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi again.

The test is still running OK when I do Run Selection on it, so I thought I'd see what happens when I put a breakpoint in the test and try to run it line by line.

So, in VSTS, I place a breakpoint and did Debug Selection. The first thing I saw was a message box pop up with the following:

Specified argument was out of range of valid values.

Parameter name: absRow.


After I clicked OK on that, I got the following in my test results window:

Failed to queue test run 'jason@QUIMBY 2008-04-09 08:52:56': Unable to start program 'C:Program FilesMicrosoft Visual Studio 9.0Common7IDEstesthost.exe'.

A Visual Studio DLL, coloader80.dll, is not correctly installed. Please repair your Visual Studio installation via 'Add or Remove Programs' in Control Panel.

If the problem persists, you can manually register coloader80.dll from the command prompt with 'regsvr32 "%CommonProgramFiles%Microsoft SharedVS7Debugcoloader80.dll"'.


I tried the test again with Run Selection and it ran fine, but the Debug Selection option always has the same error. Debug Selection even fails to work on the other test method I wrote where I used the

Dim mockSPSite As Mock = MockManager.MockObject(Of SPSite)(New String() {"http://quimby:24000"})


technique.

If you need anymore information on this then I'm more then happy to try and get it for you.

Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi,

OK, this is new (i.e. nothing we have seen in the past)

In order to investigate it we will need to reproduce it here.
If you are able to supply us a working example of this it will be great.
if not we can start by looking at the entire test case that causes this failure.

In any case lets take it offline for further investigation. (ill send you an email)
answered by lior (13.2k points)
0 votes
Hi everyone.
We fixed the problem and the solution will be added in the next release.
If anyone needs specific patch before that, we can provide it with pleasure.
answered by menahemc (220 points)
...