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

we have a large set of tests in several suites, some are low level class/objecttests/unittests and some are higher level integrative tests that test a cluster of classes.

When an integrative test (that was developed without typemock) using a class runs using class A, before a lower level test using mocked class A - the mocking of A seems to fail.

I have put in several hours to find and strip out the code that causes the issue and have reduced it to the following. It can problably be further reduced, but I feel that this will highlight the issue:

    <TestFixture()> _
    Public Class CoreConfigurationTest
        <Test()> _
        Public Sub CheckEbcHostConfiguredEx()
            Dim t As String = CoreConfigurationInternal.Instance("asda")
            'Dim t As String = CoreConfigurationInternal.Instance.Settings("asda")
            MockManager.Init()

            Dim fwkConfigMock As MockObject = MockManager.MockObject(GetType(CoreConfigurationInternal))
            Dim fwkConfig As CoreConfigurationInternal = CType(fwkConfigMock.Object, CoreConfigurationInternal)
            Dim values As NameValueCollection = New NameValueCollection
            values.Add("test", "testvalue")
            fwkConfigMock.ExpectGet("Settings", values)
            Assert.AreEqual("testvalue", fwkConfig.Settings("test"))

            MockManager.Verify()
        End Sub
    End Class


The first line of the test represents a test that is part of another assembly, another suite, run before by the testrunner (nunit-gui, nunit-console).

This test fails every other time on my setup, alternating pass/fail.
If you change the first line to access the settings (ie switch comment on line 1 & 2) it will fail every time.

If I have done something stupid, I would be happy since then I would get an easy solution! ;-)

Workarounds:
(1) Enforce that all tests reference typemock and init is called before any code is run (ie, move that pesky line below the MockManager.Init call).

This will not happen very easily in a large corp environment such as where I am right now; where we incorporate tests from many different teams and projects into the tests that are run for a component/product.

(2) Do not use typemock. (for mocking any classes that may be used in integrative tests not using typemock.)

(3) ?? (Happy to get a suggestion)


Since this exhibits the alternating pass/fail behavior I previously reported this *might* be the same problem as before.

This is a showstopper right now, I started working around by using objectseams instead of using TypeMock but I realized that I had to try to understand what is going on better because it was so similar to the problems I have had many times before with TypeMock.

I really hope this is an oversight by me or that there is a simple solution.



Couldn't attach so here is full code (like I said, can probably be narrowed down further). Create new vbproject (lib or console), add refs to nunit & typemock, paste this into class1 and run:

Imports System
Imports System.Reflection
Imports System.Configuration
Imports System.Collections.Specialized

Imports NUnit.Framework
Imports TypeMock

Namespace VBDotNetClientFrameworkIntegrationTests
    <TestFixture()> _
    Public Class CoreConfigurationTest
        <Test()> _
        Public Sub CheckEbcHostConfiguredEx()
            Dim t As String = CoreConfigurationInternal.Instance("asda")
            'Dim t As String = CoreConfigurationInternal.Instance.Settings("asda")
            MockManager.Init()

            Dim fwkConfigMock As MockObject = MockManager.MockObject(GetType(CoreConfigurationInternal))
            Dim fwkConfig As CoreConfigurationInternal = CType(fwkConfigMock.Object, CoreConfigurationInternal)
            Dim values As NameValueCollection = New NameValueCollection
            values.Add("test", "testvalue")
            fwkConfigMock.ExpectGet("Settings", values)
            Assert.AreEqual("testvalue", fwkConfig.Settings("test"))

            MockManager.Verify()
        End Sub
    End Class

    <DefaultMember("Item")> _
    Public Class CoreConfigurationInternal
        ' Properties
        Public Shared ReadOnly Property Instance() As CoreConfigurationInternal
            Get
                Return Nested.instance
            End Get
        End Property

        Default Public ReadOnly Property Item(ByVal keyName As String) As String
            Get
                If (Me.Settings Is Nothing) Then
                    Return Nothing
                End If
                Return Me.Settings.Item(keyName)
            End Get
        End Property

        Public ReadOnly Property Settings() As NameValueCollection
            Get
                Return CType(ConfigurationSettings.GetConfig("VBDotNetClientFrameworkSection"), NameValueCollection)
            End Get
        End Property


        ' Fields
        Public Const CoreSectionName As String = "VBDotNetClientFrameworkSection"

        ' Nested Types
        Private Class Nested
            ' Methods
            Shared Sub New()
                Nested.instance = New CoreConfigurationInternal
            End Sub

            ' Fields
            Friend Shared instance As CoreConfigurationInternal
        End Class
    End Class
End Namespace


asked by mawi (2k points)

2 Answers

0 votes
Hi,
This is one of the features that we are working on for next version,
I will be happy to send you an alpha version if you require it.
answered by scott (32k points)
0 votes
Hi,
This is fixed in version 3.1, where automatic initilization is implemented for known testing frameworks
answered by scott (32k points)
...