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
I'm trying to mock some ConnectionStrings

Using recorder As RecordExpectations = RecorderManager.StartRecording
recorder.ExpectAndReturn(ConfigurationManager.ConnectionStrings.Item("DB:database"), New System.Configuration.ConnectionStringSettings("DB:DocumentDataControl", "Data Source=ecserver6;Initial Catalog=pqr;User Id=userid;Password=blah;", "System.Data.SqlClient")).RepeatAlways()
End Using

However, I keep getting error. I'm pretty sure configurationmanager is not in the mscorlib. Help!

Error 1 TestCase 'CreateObjects.CreateDocumentDataControlMaster'
failed:
*** Mocked return value of ConnectionStringSettingsCollection.get_Item() is unknown, use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly.
TypeMock.TypeMockException
Message:
*** Mocked return value of ConnectionStringSettingsCollection.get_Item() is unknown, use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly.
Source: TypeMock
StackTrace:
at TypeMock.RecorderManager.d()
at TypeMock.RecorderManager.e()
at TypeMock.RecordExpectations.Dispose()
at Eutech.DocumentControl.BLUnitTests.CreateObjects.CreateDocumentDataControlMaster() in C:WebsitesEutech.Web.Application.DocumentControlEutech.DocumentControl.BLUnitTestsClass1.vb:line 72
at TypeMock.MethodDecorator.CallRealMethod()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.f()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
asked by jimmyseow123 (4.6k points)

4 Answers

0 votes
Jimmy,
have you tried doing it using the Recorder.Return() syntax?
something like:
Using recorder As RecordExpectations = RecorderManager.StartRecording
dim s as string = ConfigurationManager.ConnectionStrings.Item("DB:database")
recorder.Return( New System.Configuration.ConnectionStringSettings("DB:DocumentDataControl", "Data Source=ecserver6;Initial Catalog=pqr;User Id=userid;Password=blah;", "System.Data.SqlClient")).RepeatAlways()
End Using
answered by royo (2k points)
0 votes
Yes, I have. Same error message.
Error occurs when I try to use ConfigurationManager.ConnectionStrings.Item("DB:database")

Thanks and regards,
Jimmy
answered by jimmyseow123 (4.6k points)
0 votes
Got this working using reflective typemocks. I'm still interested in finding out how to do it using natural mocks, though. Thanks.

Dim mockedConnectionStringSettings As Mock = MockManager.Mock(Of ConnectionStringSettingsCollection)()
mockedConnectionStringSettings.ExpectGetIndexAlways(New ConnectionStringSettings("DB:DocumentDataControl", "Data Source=ecserver6;Initial Catalog=Eutechinst;User Id=ISO9000;Password=Ptxlo6vb;", "System.Data.SqlClient"))
answered by jimmyseow123 (4.6k points)
0 votes
Hi
We checked it out and it seems like we have bug that's related to the return value - ConnectionStringSettings static constructor.
The work around is to create the return value outside the recording block.
Try this:
Dim ret As ConnectionStringSettings = New System.Configuration.ConnectionStringSettings("DB:DocumentDataControl", "Data Source=ecserver6;Initial Catalog=pqr;User Id=userid;Password=blah;", "System.Data.SqlClient")

Using recorder As RecordExpectations = RecorderManager.StartRecording
   recorder.ExpectAndReturn(ConfigurationManager.ConnectionStrings.Item("DB:database"), ret).RepeatAlways()
End Using
answered by ohad (35.4k points)
...