Background:
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.
Problem:
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
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
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
Is there any way I can turn off this implicit conversion? If not, can anyone see if I'm doing something wrong?
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.
Problem:
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
Code:
<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
"Implicit conversion from ILogger to CLogger"
But the constructor it's calling is as follows
Code:
' 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
Code:
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'..
Last edited: