Skip to content

Instantly share code, notes, and snippets.

@caspian311
Created May 9, 2013 13:51
Show Gist options
  • Save caspian311/5547548 to your computer and use it in GitHub Desktop.
Save caspian311/5547548 to your computer and use it in GitHub Desktop.
Argument captures with Rhino.Mocks
using System;
using NUnit.Framework;
using Rhino.Mocks;
namespace Thing
{
public class Thing
{
private readonly IMesasgeService _messageService;
public Thing(IMesasgeService messageService)
{
_messageService = messageService;
}
public void DoIt(string name)
{
_messageService.DoSomething(broadcaster =>
broadcaster.BroadcastMessage(string.Format("Hello, {0}", name)));
}
}
public interface IBroadcaster
{
void BroadcastMessage(string message);
}
public interface IMesasgeService
{
void DoSomething(Action<IBroadcaster> action);
}
[TestFixture]
public class ThingTest
{
private Thing _testObject;
private IMesasgeService _messageService;
private IBroadcaster _broadcaster;
[SetUp]
public void Setup()
{
_messageService = MockRepository.GenerateStub<IMesasgeService>();
_broadcaster = MockRepository.GenerateMock<IBroadcaster>();
_testObject = new Thing(_messageService);
}
[Test]
public void TestThisThing()
{
_messageService.Stub(mesasgeService =>
mesasgeService.DoSomething(Arg<Action<IBroadcaster>>.Is.Anything))
.WhenCalled(method => ((Action<IBroadcaster>) method.Arguments[0])(_broadcaster));
_testObject.DoIt("Matt");
_broadcaster.AssertWasCalled(broadcaster => broadcaster.BroadcastMessage("Hello, Matt"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment