So I got a phone call this morning that went something like this....

"Yo mO, what's happenin' homie?", voice on the phone.

"Ye'.... I'm just slingin some code wit my compadre, G. What's crackin'?", says mo!

"So word on the street is that you're slingin' some made Rhino Mocks 3 dot 5 ish? So lemme asks you, how do you bust out some event raisin' with the new ish?"

My response... "I gots no clue my man, no clue!"

After some quick digging here's what I found... (Please remember this is a contrived example!)

The old school way...

 1 [TestFixture]
 2   public class AnonymousPresenterTest {
 3       private IView view;
 4       private MockRepository mockery;
 5       private ITask task;
 6   
 7       [SetUp]
 8       public void SetUp() {
 9           mockery = new MockRepository();
10           view = mockery.DynamicMock<IView>();
11           task = mockery.DynamicMock<ITask>();
12       }
13   
14       public IPresenter CreateSUT() {
15           return new AnonymousPresenter(view, task);
16       }
17   
18       [Test]
19       public void ShouldDoSomethingUseful() {
20           IEventRaiser raiser = null;
21           using (mockery.Record()) {
22               view.Load += null;
23               raiser = LastCall.GetEventRaiser();
24   
25               Expect
26                   .Call(task.AllProvinces())
27                   .Return(new List<IProvince>());
28           }
29   
30           using (mockery.Playback()) {
31               CreateSUT();
32               raiser.Raise(null, EventArgs.Empty);
33           }
34       }
35   }

Here's the new way.. that I quickly Googled for...

 1 [Concern(typeof(AnonymousPresenter))]
 2   public class when_the_view_is_first_loaded : context_spec<IPresenter> {
 3       private IView view;
 4       private ITask task;
 5   
 6       protected override IPresenter UnderTheseConditions() {
 7           view = Dependency<IView>();
 8           task = Dependency<ITask>();
 9           return new AnonymousPresenter(view, task);
10       }
11   
12       protected override void BecauseOf() {
13           view.Raise(v => v.Load += null, view, EventArgs.Empty);
14       }
15   
16       [Test]
17       public void should_do_something_useful() {
18           task.should_have_been_asked_to(t => t.AllProvinces());
19       }
20   }

So there you have it. Enjoy..

P.S.

I found the usage on Ayende's wiki here. Also, I am not a Rhino.Mocks guru, nor do I want to be, and yes the phone conversation was not as interesting as was previously illustrated.

comments powered by Disqus