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:

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

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

  
Google Buzz - Impressions

I have started to explore Google's new Buzz social enhancement to GMail. I don't consider this a twitter killer, but each system has pros and cons. I wanted to briefly put down some of my thoughts, and maybe get some feedback as well.

I Suck at 140 Characters

One of the things I struggle with on twitter is the 140 character limit. Over time I have developed a wordy style of writing that doesn't fit into a single tweet. I often spend more time trying to choose words that allow me to present my full thought than actually formulating my thought, and this frustrates me. With Buzz, I don't have that limitation. I still consider it a microblog like twitter, but one without the distraction of character limitations.

The Conversation is in your Face

One of my frustrations with twitter is following conversations. There is no simple way to do it. If you catch a response, it is fairly trivial to trace it back to the originating tweet. However, if someone asks a question, and you want to know how other people respond, the only way to do so is use twitter search. With Buzz, one person starts a thread with their "buzz", and responses are inline in the comments area. This makes it easy to see the conversation, even when it involves people you don't follow. In my opinion, this will also make buzz entries more useful in google search results.

Privacy

Currently I use twitter for my professional life, and facebook for my personal life. Sometimes one leaks into the other, but for the most part, I have made that distinction. Buzz is tied to my gmail contacts which come from mail, reader, or my android phone. This encompasses multiple facets of my life. This makes for a fuzzy line that I don't really have with the other two social services. However, buzz does have a nice security feature that makes it easy to have a post only be visible to one or more contact groups. This to me means that it can be both, and I don't have to worry about the fuzzy line. I have often wanted this ability with facebook, and am happy to see it here. The downside is now I'm going to waste hours organizing my contacts into appropriate groups.

Summary

In summary, I'm looking forward to following more people using Buzz, and seeing how discussions unfold. I could see tweets that point to buzz discussions, as I think the environment is more appropriate for that. The downside is, it doesn't have the discoverability that twitter has. If you follow someone that is involved in a conversation, you can butt in. With buzz, if you don't follow the person that started the conversation, you'll miss the whole thing. I decided not to have my tweets replicated on my buzz feed because I found the delay in google fetching the tweets makes the feature less valueabl. Also, the same post in two places would cause any discussion to be fractured.

Anyone else have any thoughts on Buzz? I think Google needs to put out an API sooner rather than later, as I can see tools like TweetDeck and Digsby integrating with Buzz, and adding value. The rollout is still in progress, but I'm curious to see what happens after the curiousity subsides.

   
O Rly? Editing Project File XML in Visual Studio

File this under things I probably should have known by now.

Whenever I have needed to edit a visual studio project file's XML, I have used an external XML/text editor. All my attempts to edit with Visual Studio resulted in the entire project being loaded, not the xml contents. So, here's the trick. First right-click on the project file in the solution explorer and select 'Unload Project'. This option will only show up if your project is a part of a solution. Once the project has been unloaded, right-click again and select the 'Edit [your project file name]' command and this will bring up the XML editor with the contents of your project file. Once the changes have been made, right-click on the project again and select 'Reload Project' to bring the project back into the solution.

  
O Rly? Intellisense requires XML + DLL

File this under things I probably should have known by now.

If you have gone through all the trouble of marking up all of your classes and methods with XML documentation so everyone who consumes your API has helpful summary and description information, make sure you deploy the .xml file with your compiled library. The documentation information is not embedded in the binary. I knew that the xml was required to generate documentation help files, I did not know that Visual Studio also required them for populating documentation in intellisense and the Object Browser.

   
Self Documenting Code - FAIL

I read some code the other day that made me do a double take.

    if(businessObject.Id.Equals(new Guid()))
    {
        // create object in db
    }

The first thing I thought was "Is this even possible? Will this code ever run?" But it will, and it does, because the default constructor on a Guid returns Guid.Empty. And while this is counter intuitive, it is the expected behavior of a default constructor on a value type.

The C# spec says that a default constructor is implicitly provided and cannot be overridden. And the behavior is to give a value produced by a bit pattern of all zeros. So this isn't a problem with the behavior of Guid, it's a problem with the behavior of the developer. Comparing to Guid.Empty is much more intuitive in terms of expected behavior.

    if (businessObject.Id.Equals(Guid.Empty))
    {
        // create object in db
    }

Please think about the poor souls who have to read your code after you are done creating it.

   
Older >
Recent Posts
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!
Stack Overflow 1K Superstar
Site Down
Git as Corporate Source Control
Resharper Vs. Smelly Code - FIGHT!
Twin Cities Code Camp 6
Tag Cloud
.net   asp.net   beta   borked   buzz   c#   clio   code   code camp   console   documentation   fail   friction   general   git   mocking   ndc09   note-to-self   opinion   orly?   r#   refactoring   resharper   sell-out   session state   skills test   sql server   stack overflow   tccc   testing   tfs   tips   troubleshooting   twitter   typemock   vb   video   visual studio   web 2.0