Any way to disable IMPLICIT CONVERSIONS in VS 2005


New member
I made an app in VS 2003 aages ago as a demonstration of unit testing (using NUnit) and mocking (using NMock). All my current (and future) work is in VS 2005 (using the VS 2005 unit testing framework) and so I thought I'd bring it up to date as I'm trying to get more people to take up the practice.

Mocking (afaik) relies on interfaces, and I tend to use constructor dependency injection of interfaces so production code can happily pass in implementation classes and my tests can inject mocks to help isolate code. I've never had any problems with 2003/NUnit but I've had an absolute nightmare trying to get it to work in 2005. I've tried NMock2, TypeMock and lastly the original NMock the old tests used.

Essentially my unit test tries to do the following

    <TestMethod()> _
    Public Sub SetPasswordTest_Positive()
        Dim pwdMgr As CPasswordManager
        Dim mockLogger As DynamicMock
        Dim mockDataStore As DynamicMock

        pwdMgr = New CPasswordManager

        mockLogger = New DynamicMock(GetType(ILogger))
        mockDataStore = New DynamicMock(GetType(IDataAccess))
        pwdMgr = New CPasswordManager([U]CType(mockLogger.MockInstance, ILogger[/U]), _
                                      [U]CType(mockDataAccess.MockInstance[/U][U], IDataAccess[/U]))
    End Sub
Which basically (I think) should work. However, the problem is that where I have underlined above, VS is showing the warning:

"Implicit conversion from ILogger to CLogger"

But the constructor it's calling is as follows

        ' Mocking Constructor - Usable for production and testing
        Public Sub New(ByVal p_logger As ILogger, ByVal p_datastore As IDataAccess)
            m_logger = p_logger
            m_datastore = p_datastore
        End Sub
So, nowhere in my test or in the production code does it specifically reference teh implementation classes CLogger or CDataAccess in any way yet VS is automatically converting the interfaces to implementation instances which causes runtime errors of type

Test method Injection_Demo_Testing.CPasswordManagerTest.SetPasswordTest_Positive threw exception:  System.InvalidCastException: Unable to cast object of type 'ProxyILogger' to type 'Injection_Demo.Injection_Demo.CLogger'..
Is there any way I can turn off this implicit conversion? If not, can anyone see if I'm doing something wrong?
Last edited:
Gah, nevermind. The IDE was screwing up and not building correctly. Now it does I've got it working with the original NMock, which is a start.

Edit: and now it works with NMock2, woo! Excuse the shoddy code, not had chance to tidy it up yet.

   Public Sub SetPasswordTest_Positive()
        Dim mocka As Mockery
        ' Define the Interfaces (dependencies) we're going to be 'injecting' into the object under test
        Dim logger As ILogger
        Dim dataAccess As IDataAccess
        ' Define the
        Dim strUser As String
        Dim strPassword As String
        ' Define the object under test
        Dim pwdmgr As CPasswordManager

        ' Instanciate the mockery
        mocka = New Mockery
        ' Instanciate dynamic mocks of the interfaces
        logger = CType(mocka.NewMock(GetType(ILogger)), ILogger)
        dataAccess = CType(mocka.NewMock(GetType(IDataAccess)), IDataAccess)
        ' Set test method parameters
        strUser = "username"
        strPassword = "passw0rd"

        ' Set expectations / responses for the mocks

        ' Instanciate the object under test
        pwdmgr = New CPasswordManager(logger, dataAccess)

        ' Call the method under test
        ' Assert that it returns the expected value (in this case, true)
        Assert.IsTrue(pwdmgr.SetPassword(strPassword, strUser))

        ' Verify all above expectations have been met
    End Sub
Last edited: