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
0 votes
When I run this test:

[TestMethod, VerifyMocks]
public void TestMethod1()
{
    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.ExpectAndReturn(global::System.Configuration.ConfigurationManager.ConnectionStrings["test"], null);
    }
    Assert.IsNull(global::System.Configuration.ConfigurationManager.ConnectionStrings["test"]);
}


I get this error:

Test method TestProject1.UnitTest3.TestMethod1 threw exception: TypeMock.TypeMockException:
*** Cannot return a value for ConnectionStringSettingsCollection.get_Item() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly..


However, this test:

[TestMethod, VerifyMocks]
public void TestMethod2()
{
    global::System.Configuration.ConnectionStringSettingsCollection connectionStrings = RecorderManager.CreateMockedObject<global::System.Configuration.ConnectionStringSettingsCollection>(Constructor.Mocked, StrictFlags.AllMethods);
    
    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.ExpectAndReturn(global::System.Configuration.ConfigurationManager.ConnectionStrings, connectionStrings);
        recorder.ExpectAndReturn(connectionStrings["test"], null);
    }
    Assert.IsNull(global::System.Configuration.ConfigurationManager.ConnectionStrings["test"]);
}


it succedes.

What am I doing wrong?
asked by paulo.morgado (11k points)

2 Answers

0 votes
       [TestMethod, VerifyMocks] 
public void TestMethod2() 
{ 
    using (RecordExpectations recorder = RecorderManager.StartRecording()) 
    {
        var ret = global::System.Configuration.ConfigurationManager.ConnectionStrings["test"];
        recorder.Return(null);
    } 
    Assert.IsNull(global::System.Configuration.ConfigurationManager.ConnectionStrings["test"]); 
}

This one is working just fine ;)
I'm not sure "ExpectAndReturn" is supposed to ChainMock calls.
answered by Gmoorick (2.3k points)
0 votes
Hi guys,

The problem here is because of a static constructor being called for ConnectionStringSettings. It's related to the issue described here:
https://www.typemock.com/community/viewtopic.php?t=813

We have a patch in progress to solve this matter. We'll send it to you once it's done.
answered by gilz (14.5k points)
...