Ben Biddington

Whatever it is, it's not about "coding"

Archive for October 2008

Mocking and interception

leave a comment »

I have Just been having a look at Moq vs RhinoMocks with respect to mocking concrete members. 

Looks like with either alternative we can mock whatever type we like, however expectations can only be set on certain types of operations on unsealed classes: virtualabstract or interface.

Moq uses Castle Dynamic Proxy which has the limitation:

You can use DynamicProxy to generate lightweight proxies on the fly for one or more interfaces or even concrete classes (but only virtual methods will be intercepted).

Hence only virtualabstract or interface operations can be invoked on a mocked instance. 

The reason for this as described by Daniel Cazzulino:

it’s a limitation of the CLR since its inception: there’s no built-in interception mechanism. so, the interception mechanism we use is auto-generated classes that inherit from the types to mock, and override all members to provide the interception. that’s why they need to be virtual. we’re reusing the interception library from Castle DynamicProxy for doing that. 

Rhinomocks is subject to the same limitation (see Capabilities section):

Attention: Rhino Mocks can only mock interfaces, delegates and virtual methods of classes! (Although it is possible to get work around existing code and easily apply Rhino Mocks, see Example of testing an abstract class)

For example, invoking the following:

MockRepository mocks = new MockRepository();
Artist list = mocks.StrictMock();

// Setup the expectation of a call on the mock
Expect.Call(list.ArtistId).Return(999);
mocks.ReplayAll();

// Evaluate the values from the mock
Assert.AreEqual(999, list.ArtistId);
mocks.VerifyAll();

Results in an error: 

Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

And mocking an abstract class requires slightly more work.

Mocking System.Object with Rhino Mocks

The following snippet fails:

MockRepository mocks = new MockRepository();
Object mockObject = mocks.StrictMock();

Expect.Call(mockObject.Equals(null)).Return(false);

mocks.ReplayAll();

With exception:

Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

And yet if I create a type, have it extend System.Object and override GetHashCode:

public class ObjectWrapper : Object
    {
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

 

then that same snippet works. Is this related to the mscorlib thing that TypeMock has?

Advertisements

Written by benbiddington

31 October, 2008 at 09:33

Programming

leave a comment »

There is a good article here with a bunch of links.

Also lately I have been listening to podcasts from Software Engineering Radio, particularly the ones on EBay’s architecture, relational databases and database transactions.

And I have ordered Hacker & Painters,  and a new piece of meat.

Written by benbiddington

29 October, 2008 at 10:18

Posted in development

Tagged with ,

TypeMock bug discovered

leave a comment »

Today we found TypeMock.Init() began to fail with an error like:

Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure “c2.resources” was correctly embedded or linked into assembly “TypeMock” at compile time, or that all the satellite assemblies required are loadable and fully signed.

It turned out this was due to nag screen which is supposed to display when the trial has seven days to run. The nag screen could not be initialized due to a missing resource file.

We posted in the bug forum, and a moderator has confirmed they will issue a fix.

I discovered this by examining the assembly with Reflector. As a temporary fix we were able to also find the registry key used for the date, delete it and reinstall — this reset the nag to 21 days.

Written by benbiddington

14 October, 2008 at 18:24

Posted in Uncategorized

Tagged with , , ,

Configuring routes in ASP.NET MVC

leave a comment »

Routes can be configured as follows:

routes.Add(
    new Route(
        "files/{id}",                        // URL pattern
        new RouteValueDictionary    // Defaults
        {
            { "controller"  , "file"},      // Default controller is file
            { "action"      , "index"},
            { "id"          , null}
        },
        new MvcRouteHandler()
    )
);

Which means an URL like:

/files/12345

returns the file with id equal to 12345.

Written by benbiddington

8 October, 2008 at 08:31

Posted in Uncategorized

Tagged with , ,