Thoughts on Software by Andrew Davey
 Monday, July 02, 2007
Rhino Mocks DSL in Boo

I recently learnt that Boo now supports a DSL-friendly syntax. A while back I created (and almost finished!) a set of syntactic macros in Boo to make using Rhino Mocks much more natural. Creating macros can be hard work though and gets pretty hacky in places.

The new DSL-friendly syntax in Boo means I can achieve 80% of the same easy-reading code, but using only methods and blocks!

In about 10 minutes I whipped up this:

namespace ConsoleDemo

import System
import Rhino.Mocks
import Rhino.Mocks.BooDsl.MockDsl

interface IModel:
    def CalculateScore(input as int) as int
interface IView:
    def DisplayScore(score as int)
interface IPresenter:
    def Init()

class Presenter(IPresenter):
    _model as IModel
    _view as IView
    def constructor(model as IModel, view as IView):
        _model = model
        _view = view
    def Init():
        score = _model.CalculateScore(0)
        _view.DisplayScore(score)

def Test_presenter_init():
    with_mocks:
        // Create mocks of the dependencies
        model = Mocks.CreateMock of IModel()
        view = Mocks.CreateMock of IView()
        // Create the object we are testing
        presenter = Presenter(model, view)
        // Record what we expect the presenter to
        // do with its dependencies.
        record:
            expect:
                model.CalculateScore(0)
                // Set the return value for the
                // previous mock call.
                mock_return 42
            expect:
                view.DisplayScore(42)
        // Now tell the presenter to do its stuff,
        // so we can verify it behaves correctly.
        verify:
            presenter.Init()
print "testing..."
Test_presenter_init()
print "all good!"
print "Press any key to continue . . . "
Console.ReadKey(true)

The import of "Rhino.Mocks.BooDsl.MockDsl" brings some new methods into scope. with_mocks initializes a MockRepository. "Mocks" is actually a property referencing that repository, so we can call CreateMock, etc, on it. The record and verify methods make use of the Mocks.Record() and Mocks.Playback() methods. I prefer using "verify" to "playback". The "expect" method runs the code in its block, usually that invokes some mock method. It then calls "LastCall.Repeat.Once()" to set up the expectation. The "mock_return" method calls "LastCall.Return(value)" to set up the return value for the last mock call.

This is all a bit messy for now I know. I have just being throwing code down into this prototype to get a feel for what is possible. I reckon this could be really useful for people using Rhino Mocks.

If you like what you've seen or have any ideas let me know.

The DSL methods are as follows:
I am aware of more than one glaring danger in the code(!) but it does show what can be done.

namespace Rhino.Mocks.BooDsl

import System
import Rhino.Mocks

callable Block()

static class MockDsl:

    private static _instance as MockDslImpl
    static def with_mocks(b as Block):
        _instance = MockDslImpl()
        b()
        _instance = null
    static def record(b as Block):
        assert _instance != null
        _instance.Record(b)
    static def verify(b as Block):
        assert _instance != null
        _instance.Verify(b)
    static def expect(b as Block):
        assert _instance != null
        _instance.Expect(b)
    static def mock_return(value as object):
        assert _instance != null
        _instance.MockReturn(value)
    static Mocks as MockRepository:
        get:
            assert _instance != null
            return _instance.Mocks

    private class MockDslImpl:
        mocks as MockRepository
        def constructor():
            mocks = MockRepository()
        public Mocks as MockRepository:
            get:
                return mocks
        public def Record(b as Block):
            using mocks.Record():
                b()
        public def Verify(b as Block):
            using mocks.Playback():
                b()
        public def Expect(b as Block):
            b()
            LastCall.Repeat.Once()
        public def MockReturn(value as object):
            LastCall.Return(value)


Monday, July 02, 2007 11:03:52 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   |  |  |  |  | Tracked by:
"Rambled Thoughts" (Ayende @ Rahien) [Trackback]
http://morningside.edu/mics/_notes/pages/paxil/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cymbalta/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/hoodia/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lipitor/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/clomid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/clomid/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/synthroid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/nexium/index.html [Pingback]
http://blastpr.com/wiki/js/pages/hoodia/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prilosec/index.html [Pingback]
http://blastpr.com/wiki/js/pages/synthroid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cymbalta/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/wellbutrin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/zoloft/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/nexium/index.html [Pingback]
http://blastpr.com/wiki/js/pages/soma/index.html [Pingback]
http://blastpr.com/wiki/js/pages/tramadol/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celebrex/index.html [Pingback]
http://blastpr.com/wiki/js/pages/coumadin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/accutane/index.html [Pingback]
http://blastpr.com/wiki/js/pages/effexor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/wellbutrin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/coumadin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/melatonin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/ultram/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/tramadol/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/viagra/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/soma/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cialis/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celexa/index.html [Pingback]
http://blastpr.com/wiki/js/pages/prozac/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prozac/index.html [Pingback]
http://blastpr.com/wiki/js/pages/prilosec/index.html [Pingback]
http://lecouac.org/ecrire/lang/docs/25282359/index.html [Pingback]
http://islands-croatia.comislands-croatia.com/includes/js/docs/87090382/index.ht... [Pingback]
http://pddownloads.com/docs/94929363/index.html [Pingback]
http://plantmol.com/docs/24471383/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/48335156/index.html [Pingback]
http://coolioness.com/attachments/docs/75395149/index.html [Pingback]
http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html [Pingback]
http://realestate.hr/templates/css/docs/71546796/index.html [Pingback]
http://swellhead.netswellhead.net/docs/92808772/index.html [Pingback]
http://temerav.com/images/menu/96509501/index.html [Pingback]
http://thebix.com/includes/compat/docs/10152421/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/70471394/index.html [Pingback]
http://legambitdufou.org/Library/docs/28049195/index.html [Pingback]
http://thebix.com/includes/compat/docs/29852280/index.html [Pingback]
http://pspdesktops.com/fileupload/store/docs/33460308/index.html [Pingback]
http://allfreefilms.com/wp-includes/js/25891222/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/63224938/index.html [Pingback]
http://thebix.com/includes/compat/docs/44694113/index.html [Pingback]
http://seo4u.at/images/docs/68897595/index.html [Pingback]
http://lecouac.org/ecrire/lang/docs/49649526/index.html [Pingback]
http://thebix.com/includes/compat/docs/15132509/index.html [Pingback]
http://pspdesktops.com/fileupload/store/docs/04061117/index.html [Pingback]
http://islands-croatia.comislands-croatia.com/includes/js/docs/60974094/index.ht... [Pingback]
http://lecouac.org/ecrire/lang/docs/77066936/index.html [Pingback]
http://ipsilon.hr/ipsilon.hr/cms/4/lib/docs/55227677/index.html [Pingback]
http://add2rss.com/img/design/docs/90861918/index.html [Pingback]
http://jivest2006.com/docs/40579018/index.html [Pingback]
http://martinrozon.com/images/photos/docs/54373182/index.html [Pingback]
http://seo4u.at/images/docs/76783685/index.html [Pingback]
http://legambitdufou.org/Library/docs/64933533/index.html [Pingback]
http://hrvatska.biz/wp-includes/js/docs/80692203/index.html [Pingback]
http://discussgod.com/cpstyles/docs/62161481/index.html [Pingback]
http://jivest2006.com/docs/42940613/index.html [Pingback]
http://coolioness.com/attachments/docs/76375390/index.html [Pingback]
http://slaterjohn.com/downloads/2col/51579700/index.html [Pingback]
http://legambitdufou.org/Library/docs/38152786/index.html [Pingback]
http://pddownloads.com/docs/08296030/index.html [Pingback]
http://promocija.com.hr/promocija.com.hr/includes/js/docs/37348396/index.html [Pingback]
http://coolioness.com/attachments/docs/60340594/index.html [Pingback]
http://split-dalmatia.com/split-dalmatia.com/images/docs/34320152/index.html [Pingback]
http://temerav.com/images/menu/05559064/index.html [Pingback]
http://pddownloads.com/docs/21991908/index.html [Pingback]
http://witze-humor.de/templates/images/docs/69259068/index.html [Pingback]
http://coolioness.com/attachments/docs/83777724/index.html [Pingback]
http://thejohnslater.com/pix/img/docs/56008043/index.html [Pingback]
http://entartistes.ca/images/images/docs/65934120/index.html [Pingback]
http://allfreefilms.com/wp-includes/js/46226552/index.html [Pingback]
http://adventure-traveling.com/images/img/viagra/ [Pingback]
http://sevainc.com/bad_denise/img/10/soma/ [Pingback]
http://ina-tv.sk/img/cialis/ [Pingback]
http://easytravelcanada.info/js/pages/12/viagra/ [Pingback]
http://easycanada.info/js/pages/viagra/ [Pingback]
http://easytravelcanada.info/js/pages/3/claritin/ [Pingback]
http://abaffy.net/i/img/viagra/ [Pingback]
http://easytravelcanada.info/js/pages/8/paxil/ [Pingback]
http://sevainc.com/bad_denise/img/5/hoodia/ [Pingback]
http://easytravelcanada.info/js/pages/6/lexapro/ [Pingback]
http://sevainc.com/bad_denise/img/9/rainbow-brite/ [Pingback]
http://easytravelcanada.info/js/pages/12/wellbutrin/ [Pingback]
http://sevainc.com/bad_denise/img/7/melatonin/ [Pingback]
abaffy.org/la/img/cialis/ [Pingback]
http://easytravelcanada.info/js/pages/7/nexium/ [Pingback]
http://easytravelcanada.info/js/pages/5/effexor/ [Pingback]
http://adventure-traveling.com/images/img/cialis/ [Pingback]
http://easytravelcanada.info/js/pages/1/celebrex/ [Pingback]
http://easytravelcanada.info/js/pages/2/celexa/ [Pingback]
http://easytravelcanada.info/js/pages/8/prilosec/ [Pingback]
http://easytravelcanada.info/js/pages/7/melatonin/ [Pingback]
http://birds.sk/img/cialis/ [Pingback]
http://sevainc.com/bad_denise/img/1/accutane/ [Pingback]
http://simplecanada.info/js/pages/13912893/ [Pingback]
http://easytravelcanada.info/js/pages/3/clomid/ [Pingback]
http://easytravelcanada.info/js/pages/5/hoodia/ [Pingback]
http://sevainc.com/bad_denise/img/11/ultram/ [Pingback]
http://easytravelcanada.info/js/pages/12/zoloft/ [Pingback]
http://sevainc.com/bad_denise/img/10/synthroid/ [Pingback]
http://sevainc.com/bad_denise/img/12/zoloft/ [Pingback]
http://easytravelcanada.info/js/pages/4/cymbalta/ [Pingback]
http://easymexico.info/images/img/cialis/ [Pingback]
http://ina-tv.sk/img/viagra/ [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/ravon-nude.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/inspirational-business-sta... [Pingback]
http://odin.net/images/pages/52807681/sex-as-a-suspect-classification.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-swim-crest.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/free-erotic-lesbian-video.... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/free-sex-positions-clips.h... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/xxx-asian-anal-milf-free.h... [Pingback]
http://odin.net/images/pages/52807681/sex-and-deviltry.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/denise-davies-anal.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/jacqueline-teen-model-is-n... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/andy-kim-baby-i-love-you.h... [Pingback]
http://odin.net/images/pages/52807681/lolita-boy-nude-pics.html [Pingback]
http://odin.net/images/pages/35694472/small-tit-teens-tgp.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/pre-teen-posing-nude.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/nude-celeb-thumbs.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/sex-pussy-dick.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/underwater-girl-nude.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/virgin-vagina-pic.html [Pingback]
http://odin.net/images/pages/35694472/baby-got-back-by-throwdown.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/bikini-dare-pics.html [Pingback]
http://odin.net/images/pages/52807681/cheerleaders-sex-towel.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/old-film-girl-in-love-with-... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/pics-of-sexy-women-in-west... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/agent-scully-alien-fuck.ht... [Pingback]
http://odin.net/images/pages/35694472/columbia-adult-escorts-south-carolina.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/mature-whore-fisting.html [Pingback]
http://odin.net/images/pages/35694472/art-bdsm.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/baby-pool.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/anime-preteen-sex.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/college-girls-escorts.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/sexual-protective-strategie... [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/girl-teen-underwear.html [Pingback]
http://odin.net/images/pages/52807681/teen-fashion.html [Pingback]
http://odin.net/images/pages/52807681/aurora-snow-xxx.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/cheerleader-erotic-stories.... [Pingback]
http://odin.net/images/pages/35694472/xxx-schoolgirls-hardcore-pictures.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/little-match-girl-story.ht... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/free-mature-bbw-porn.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/dylan-scott-xxx.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/35807953/teenage-girl-nude.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/i-need-free-party-ideas-for... [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/a1-thumbnails-posts.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/sexy-makeup-pics.html [Pingback]
http://odin.net/images/pages/52807681/index.html [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/the-internet-is-for-porn.h... [Pingback]
http://gatewayplayhouse.com/photos/cai/pages/53348735/barbue-girl.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/xpress-train-hentai-movie.h... [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/statistics-on-teens-allowan... [Pingback]
http://cidesi.com/images/metro/metro2/pages/99493954/view-free-sex-scenes.html [Pingback]
http://cidesi.com/images/metro/metro2/pages/32162341/cards-adult-humor.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-vicodin-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-cialis-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-viagra-online.html [Pingback]
http://www.signalprocessingsociety.org/community/forum/buy-phentermine-online.ht... [Pingback]


Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):