Problems Mocking the ConfigurationManager.AppSettings

6 posts, 1 answers
  1. Joel
    Joel avatar
    19 posts
    Member since:
    Dec 2011

    Posted 13 Dec 2011 Link to this post

    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)>.

  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 14 Dec 2011 Link to this post

    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 >>

  3. DevCraft R3 2016 release webinar banner
  4. Joel
    Joel avatar
    19 posts
    Member since:
    Dec 2011

    Posted 14 Dec 2011 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 15 Dec 2011 Link to this post

    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 >>

  6. Murugesh
    Murugesh avatar
    1 posts
    Member since:
    Jun 2012

    Posted 28 Jun 2012 Link to this post

    Hi,

    Does this problem still exist with MSTest? Any fixes?

    Thanks,
    Murugesh.
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 04 Jul 2012 Link to this post

    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 >>

Back to Top
DevCraft R3 2016 release webinar banner