Mocking concrete classes using Rhino.Mocks
Pretty much all my agile-aware developer life I have been using Rhino.Mocks to mock interfaces. I had never landed myself in a position where I had to mock a concrete class. Being a Design Purist (well , at least when I review designs), I never liked the idea behind mocking concrete classes Consider a concrete class MyAdder, which I would like to mock. It’s a utility class which can get as trivial as it can. Which would return a sum of two integers x & y.
namespace MyMathLibrary
{
public
class
MyAdder
{
public
int Add(int x, int y)
{
return x + y;
}
}
}
The Unit Test for MyAdder, simple unit Test case
namespace MyMathLibrary.UnitTest
{
// I’m writing my usings here.. TY StyleCop ![]()
using NUnit.Framework;
using Rhino.Mocks;
[TestFixture]
public
class
MyAdderTest
{
[Test]
public
void AddTest()
{
MockRepository mockery = new
MockRepository();
MyAdder mockAdder = mockery.StrictMock<MyAdder>();
// use mockery.CreateMock<> for older versions.
// Return int 5 for all calls..
Expect.On(mockAdder).Call(mockAdder.Add(2, 3));
LastCall.IgnoreArguments().Return(5).Repeat.Any();
mockery.ReplayAll();
int sum = mockAdder.Add(1, 3);
int sum2 = mockAdder.Add(4,3);
Assert.AreEqual(5,sum);
Assert.AreEqual(5,sum2);
}
}
}
But the test would fail & the Error would look something like this
System.InvalidOperationException: There is no matching last call on this object.
Are you sure that the last call was a virtual or interface method call?
at Rhino.Mocks.Impl.RecordMockState.GetLastMethodOptions<T>()
at Rhino.Mocks.MockRepository.LastMethodCall<T>(Object mockedInstance)
at Rhino.Mocks.Impl.CreateMethodExpectation.Call<T>(T ignored)
at MyMathLibrary.UnitTest.MyAdderTest.AddTest()
If you still haven’t figured out, the error. This is because, methods which you would mock from the concrete class should be marked with virtual.And unlike Java by default every method is non virtual. This is a drawback of mocking concrete classes & this is something you will have to live with.
Here’s the new MyAdder class with the black magic & voodoo to run the test cases.
namespace MyMathLibrary
{
public
class
MyAdder
{
public
virtual
int Add(int x, int y)
{
return x + y;
}
}
}
Further read – http://codebetter.com/blogs/jeremy.miller/archive/2006/02/20/138730.aspx
“Windows Cloud” in four weeks.. ??
Well I never thought I would blog about this. (Who am I kidding ? )atleast not now. We have been hearing a lot of cacophony from the Microsoft Stable about Surface Computing and Microsoft’s very own Microsoft Surface. According to these articles in The Register & OSNews Microsoft did confirm the release of Windows Could by the end of this month (Oct ’2008).
In an interesting development to the story Microsoft Boss Steve Ballmer (and yeah I’m laughing too) of Microsoft was heard saying
- “We need a new operating system designed for the cloud and we will introduce one in about four weeks, we’ll even have a name to give you by then. But let’s just call it for the purposes of today ’Windows Cloud’”
- “Just like Windows Server looked a lot like Windows but with new properties, new characteristics and new features, so will Windows Cloud look a lot like Windows Server.”
- “We’re not driving an agenda towards being service providers but we’ve gotta build a service that is Windows in the cloud,”
- “If you talk to Google they’ll say it’s thin client computing but then they’ll issue a new browser that’s basically a big fat operating system designed to compete with Windows but running on top of it”
- “The last thing we want is for somebody else to obsolete us, if we’re gonna get obseleted we better do it to ourselves.”
And how often does a Microsoft related article appear on Google Blogscoped ?
Microsoft Style Cop 4.3 Released
Microsoft Style Cop 4.3 – Code Gallery Home Page
Microsoft Style Cop on MSDN Blogs
All along the road .NET programmers have been damned for the lack of having a coding style monitoring tools analogous to CheckStyle for Java. More often than not we tend to lean towards for other tools like Microsoft FxCop (which is now packaged as a part of Visual Studio from VS2008 SP1 onwards, which I personally think they were a release or two late in doing that.)
Sometime around May ’08 a MSFT employee called Jason Allor released a version of Style Cop (Then called the Microsoft Source Analysis Tool) which was used by Microsoft internally (see here). The initial version was somewhat incomplete without Documentation or an SDK to author custom tools. But since its 4.3 release which comes a neatly written Documentation & an SDK.
Style Cop installs as an Add-in to Visual Studio. And has a pretty aggressive default setting for coding standards.
A simple tutorial to get our feet wet will be posted in my next blog post. So Stick around !!
Dependency Injection
FAQs
What the heck is Dependency Injection?
Can you give me an analogy?
err.. semi-kolan ..??.. Really..??
Well, after a long thought process of what to name my new tech-blog. I have finally decided to name semi-kolan. Yes inspired from the fave character of programmers. The SemiColon !!.
Well I plan to document my experiments, views and discussions mostly about C#, .NET, Visual Studio. And related emerging technologies.
Stick around for more.
Next Post: Dependency Injection..!!