This is a migrated thread and some comments may be shown as answers.

Problems Mocking the ConfigurationManager.AppSettings

5 Answers 1775 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 13 Dec 2011, 09:14 PM
I'm currently using the trial of JustMock.  I created a fresh JustMock Test project and I'm unable to mock the ConfigurationManger AppSettings.

This is the code I'm using:


[TestMethod]
public void TestMethod1()
{
    // Arrange
    var appSettings = new NameValueCollection { { "test1", "one" } };
 
    Mock.Arrange(() => ConfigurationManager.AppSettings)
        .Returns(appSettings)
        .MustBeCalled();
 
    // Act
    var test1 = ConfigurationManager.AppSettings["test1"];
 
    // Assert
    Assert.IsTrue(Mock.IsProfilerEnabled);
    Assert.AreEqual("one", test1);
}


This is the error I receive: 

Failed TestMethod1 JustMockTest Assert.AreEqual failed. Expected:<one>. Actual:<(null)>.

5 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 14 Dec 2011, 01:02 PM
Hi Joel,

Thanks again for sending the issue. However, i found that it’s happening in MSTest only. I did the same test using NUnit and it passed. Therefore, it looks like MsTest is intercepting the ConfigruationManager.AppSettings for which JM can’t intercept it; as it has already fired the OnJITCompilationStarted event.

I have further attached my test project to let you have a look. Here i have used TestDrvien.Net RTM as runner.

Kind Regards,
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Joel
Top achievements
Rank 1
answered on 14 Dec 2011, 08:44 PM
Ahh.  Yes I am using MSTest.

What is the recommended test tool to use for JustMock?

btw, so far I'm really liking JustMock.  It has solved many problems we've had with our testing and has allowed us to drastically increase code coverage!

- j
0
Ricky
Telerik team
answered on 15 Dec 2011, 09:37 AM
Hi Joel,

Thanks again for the prompt reply. Honestly, there is no specific recommendation for JustMock but in terms of being light-weight and supporting wide variety of test runners I personally recommend NUnit with the Should library (available in NuGet). Following that, the second choice could be XUnit
 
It's great to hear that you are enjoying the product.

Kind Regards,
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Murugesh
Top achievements
Rank 1
answered on 29 Jun 2012, 12:29 AM
Hi,

Does this problem still exist with MSTest? Any fixes?

Thanks,
Murugesh.
0
Ricky
Telerik team
answered on 04 Jul 2012, 03:14 PM
Hi Murugesh,

Thanks again for contacting us. 

However, as we have shown in the example its because of MSTest accessing the ConfigurationManger.AppSettings before JustMock thus raises the OnJITCompilationStarted event and prevents JM from intercepting it. 

However, I tried the same test with VS2011 and it worked perfectly. The issue so far seems to be resolved in VS2011.


Kind Regards,
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Joel
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Joel
Top achievements
Rank 1
Murugesh
Top achievements
Rank 1
Share this question
or