Moq and VB.Net are Frenemies

Mocking is an important aspect of writing unit tests. The current project that I am on does a lot of integration testing, but not much unit testing. I'm trying to increase the amount of true unit tests, but struggle to do so due to the difficulty in mocking subroutines.

The team that I am on had selected Moq as the mocking framework of choice prior to my arrival, however there aren't actually any tests using it. As long as I am mocking functions on an interface, Moq is an acceptable framework, however subroutines are completely unmockable. I have done a fair amount of googling, and there seems to be quite a few questions on Stackoverflow about it, but no real answers.

In this particular case, what I want to do with mocking is ensure that the proper subroutine is called when I pass in an object that matches certain criteria. I am able to get it to work with Rhino Mocks by doing the following:

    <TestMethod()> _
    Public Sub SaveProposal_WhenProposalIsNew_CallCreate()
        Dim proposal = New Proposal("title", "description")
        Dim dataProvider = MockRepository.GenerateMock(Of IProposalDataProvider)()
        dataProvider.Expect(Function(dp) CreateProposal(dp)).IgnoreArguments()
 
        Dim service = New ProposalService(dataProvider, TestAppContext.Current)
        service.SaveProposal(proposal)
 
        dataProvider.VerifyAllExpectations()
    End Sub
 
    Private Function CreateProposal(ByVal dp As IProposalDataProvider)
        dp.CreateProposal(Nothing)
        Return Nothing
    End Function

However, when I try something similar with Moq using the code below:

    <TestMethod()> _
    Public Sub SaveProposal_WhenProposalIsNew_CallCreate()
        Dim proposal = New Proposal("title", "description")
        Dim dataProvider = New Mock(Of IProposalDataProvider)()
        dataProvider.Setup(Function(dp As IProposalDataProvider) CreateProposal(dp))
 
        Dim service = New ProposalService(dataProvider.Object, TestAppContext.Current)
        service.SaveProposal(proposal)
 
        dataProvider.VerifyAll()
    End Sub
 
    Private Function CreateProposal(ByVal dp As IProposalDataProvider)
        dp.CreateProposal(It.IsAny(Of ProposalDto))
        Return Nothing
    End Function

I get the following exception:

System.ArgumentException: Invalid setup on a non-overridable member: dp => value(ProposalManagerApp.Tests.ProposalServiceTests).CreateProposal(dp).

Any mock-magicians out there know the answer? The problem stems from the inability of VB to create an Expression(Of Action(Of Something)) because lambdas must return a value. The universe plans on correcting itself with the release of VB10, by allowing Sub() lambdas, but I can't wait that long.

  
Comments
Why don't you switch to RhinoMocks? You already stated that there are not many tests that are using Moq, and the arguments for using Moq in general have dwindled with more recent releases of RhinoMocks.

Just a thought...
Up until yesterday, I was the only one using a mocking framework in tests. I would have gladly started using Rhino Mocks and rewritten all my tests to do so, but I just so happened to encourage another dev to start using mocks as well, so now I would be forcing him to learn a new tool, 1 day after learning the previous tool, and rewrite his tests. Bad timing on my part :(
This write-up is precise and justify the time it'll consume while reading it. I will propose each and every one browsing this issue must have a search on this post. It has all the key points in regards to the theme and covers all of the elements connected for the topic.
I appear here all the time and am incredibly pleased with your updates.Thank you extremely a lot for your exceptional reality about this great subject. I could not detect this type of dissertation strategy in Internet and wanted to pay for the thesis.
Good news,here will troduce nike shoes air jordan shoes to everyone.
air jordan 2009,air jordan 2010,
air jordan 1,
air jordan 2,air jordan 3,air jordan 4,air jordan 5...air jordan 23,
jordan ajf,jordan retro,these series of
air jordan shoes are popular this year,in the same time,nike air jordan
ltd,jordan classic 87,jordan classic 91 these nike air jordan shoes also hot sell.in the urjordan.com also have womens
air jordan,just like womens air jordan
2009,womens air jordan 2010 not only these shoes,the website have nike kobe shoes,nike lebron shoes and Kevin Durant Shoes.the nike kobe brybant
shoes have kobe v shoes,nike kobe 5.5 shoes,nike kobe IV shoes,Nike Hyperdunk Shoes,nike kobe VI shoes;nike lebron
James shoes have nike lebron VII shoes,nike lebron VII low shoes,Nike Lebron VII ps shoes,nike lebron VIII(8)
shoes,these are the hot sell in the world.last want want to tell u about nike free shoes and air jordan shoes.Now i
recommend you can look air jordan 2009
and air jordan 2010 jordan shoes blog!THANK U!
Add Your Comment
Your email address will never be displayed anywhere.
Name:
Email:
Website:
Comment:
Type the word 'banana':
Blog Home
Recent Posts
Clearing the ClickOnce App Cache
VB.NET Gotchas for C# Devs
Dreamhost 1 year Special
Can I Suggest Bit Torrent?
Moq and VB.Net are Frenemies
Google Buzz - Impressions
O Rly? Intellisense requires XML + DLL
Self Documenting Code - FAIL
The 'F' in TFS is for 'Friction'
NDC 2009 Videos
Skills Test - FAIL!