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
Dear All,
I'm very new to TypeMock. Tried to mock AppSettings but ran into some trouble. Can't seem to get it working.
I want to mock the ConfigurationSettings.AppSettings so that it always returns a specified string. Unfortunately, the test always fails with error code "TestCase 'NUnitTests.TestSub' failed: TypeMock.TypeMockException : No method AppSettings in type System.Configuration.ConfigurationSettings returns System.String"
I've looked in the framework documentation and I found that AppSettings returns a NameValueCollection and not a string. Is that why the tests are failing? Is there any other way? Thanks a lot for any help. :D

<NUnit.Framework.Test()> _
Public Sub TestSub()
Dim ClassUnderTest As New ClassToBeTested

MockManager.Init()
Dim appSettingsMock As Mock = MockManager.Mock(GetType(ConfigurationSettings))
appSettingsMock.AlwaysReturn("AppSettings", "Returned String to be compared.")
NUnit.Framework.Assert.AreEqual(ClassUnderTest.ReturnParameter, "Returned String to be compared.")
MockManager.Verify()

End Sub

End Class


Public Class ClassToBeTested
Public Function ReturnParameter()
Dim result As String = ConfigurationSettings.AppSettings("asdfsdf")
Return result
End Function
End Class
asked by jimmyseow123 (4.6k points)

6 Answers

0 votes
Hi Jimmy
I'm very new to TypeMock.
Welcome :-)
I want to mock the ConfigurationSettings.AppSettings so that it always returns a specified string. Unfortunately, the test always fails with TypeMock.TypeMockException : No method AppSettings in type System.Configuration.ConfigurationSettings returns System.String"

I've looked in the framework documentation and I found that AppSettings returns a NameValueCollection and not a string. Is that why the tests are failing? Is there any other way? Thanks a lot for any help. :D

You are correct! you are trying to return a string instead of a NameValueCollection. This won't work.

There are a few ways to mock this scenario. As follows
1. Mock NameValueCollection
2. Create your own NameValueCollection and mock ConfigurationSettings.AppSettings to return it
3. Create a mocked NameValueCollection and mock ConfigurationSettings.AppSettings to return it

Here are the examples returning "TypeMock":

:arrow: 1. Mock NameValueCollection - Note: We are mocking an Index
Dim valueCollectionMock As Mock = MockManager.Mock(GetType(NameValueCollection))
valueCollectionMock.ExpectGetIndexAlways("TypeMock")


:arrow: 2. Create your own NameValueCollection and mock ConfigurationSettings.AppSettings to return it
- Note: AppSettings is a property
' Create our own NameValueCollection
Dim valueCollection As NameValueCollection = New NameValueCollection
valueCollection("expectedKey") = "TypeMock"

'  AppSettings should return our collection
Dim appSettingsMock As Mock = MockManager.Mock(GetType(ConfigurationSettings)) 
appSettingsMock.ExpectGetAlways("AppSettings",valueCollection)


:arrow: 3. Create a mocked NameValueCollection and mock ConfigurationSettings.AppSettings to return it
' Create our own mocked NameValueCollection
' We use MockObject as we need the actual instance
Dim valueCollectionMock As Mock = MockManager.MockObject(GetType(NameValueCollection))
valueCollectionMock.ExpectGetIndexAlways("TypeMock")
' Get the actual instance
Dim valueCollection As NameValueCollection = valueCollectionMock.Object

'  AppSettings should return our collection
Dim appSettingsMock As Mock = MockManager.Mock(GetType(ConfigurationSettings)) 
appSettingsMock.ExpectGetAlways("AppSettings",valueCollection)


Each method has its own advantages.
:idea: Method 1. Is the easiest although if NameValueCollection is used in other places we will get strange results.

:idea: Method 2. Is a bit better, but we have to know the exact names of keys that we expect to have (This is not such a bad resriction but you need to return the same string for all keys..)

:idea: Method 3. Is a combination of 1 & 2. We mock NameValueCollection but use it only in our context

Hope this helps
answered by scott (32k points)
0 votes
That's great!!
I was eagerly waiting for answer. I'm really excited about the possibilities of TypeMock.
Your reply was fast and clear.

Thanks very much
answered by jimmyseow123 (4.6k points)
0 votes
I'm disappointed now.
I tried your suggestion but I couldn't find the ExpectGetIndexAlways routine, so I downloaded and installed V2.3.
Unfortunately, I still can't find ExpectGetIndexAlways even though it was documented in the API. Is that going to come with V2.4?
Also, V2.3 doesn't seem to work with TestDriven.Net 1.0.915. Seems to keep crashing. I had no problems with V2.1 and Testdriven.net.

Thanks for any help.
answered by jimmyseow123 (4.6k points)
0 votes
Sorry, please disregard my last posting. Exception was caused because I didn't replace the old TypeMock.dll with the new TypeMock.dll in the Visual Studio project that I was testing :oops:

I tried your suggestions
Method 1 didn't work. It returned the following error.
"No method set_Item in type System.Collections.Specialized.NameValueCollection returns System.String "

Method 2 works fine.

Method 3 - the compiler complained about valueCollectionMock.Object - "Object is not a member of TypeMock.Mock"
answered by jimmyseow123 (4.6k points)
0 votes
I didn't replace the old TypeMock.dll with the new TypeMock.dll in the Visual Studio project

Jimmy,
As the typemock.dll is in the GAC you don't have to copy it, (in Visual Studio -> Choose the TypeMock reference -> right click properties and set Copy Local to False)


Method 1 didn't work. It returned the following error.
"No method set_Item in type System.Collections.Specialized.NameValueCollection returns System.String "

True this is a small :twisted: bug (Will be in next fix), you can work around it by:
valueCollectionMock.AlwaysReturn("get_Item","TypeMock") 

But this method might work, because the AppSettings are set before TypeMock is Init()

Method 3 - the compiler complained about valueCollectionMock.Object - "Object is not a member of TypeMock.Mock"

Sorry we should Dim As MockObject and not Mock
Dim valueCollectionMock As MockObject = MockManager.MockObject(GetType(NameValueCollection)) 
valueCollectionMock.AlwaysReturn("get_Item","TypeMock")
'  AppSettings should return our collection
Dim appSettingsMock As Mock = MockManager.Mock(GetType(ConfigurationSettings))
appSettingsMock.ExpectGetAlways("AppSettings",valueCollectionMock.Object) 
answered by scott (32k points)
0 votes
Thanks for your help!
I've been using the second method with great results.
Looking forward to improving my testing in leaps and bounds.
answered by jimmyseow123 (4.6k points)
...