|
From: Owen R. <OR...@th...> - 2003-07-13 13:46:33
|
as i've been doing a bunch of work lately with remoting, i wanted a way to
test my remotable interfaces and the code that called them. to facilitate
this, i've augmented nmock to serve remotable mocks. this basically
entails two things:
1) generating dynamic mocks that extend MarshalByRef so that they can be
serialised and proxy'ed.
2) providing a simple server to host the mocks
here's a sample test to show you how it works:
[Test]
public void MarshalRemotingMock()
{
RemotingMock mock = new RemotingMock(typeof(Foo));
mock.Expect("Bar");
TcpChannel channel = new TcpChannel(1234);
using (MockServer server = new
MockServer(mock.MarshalByRefInstance, channel, "mock.rem"))
{
Foo foo =
(Foo)RemotingServices.Connect(typeof(Foo),
"tcp://localhost:1234/mock.rem");
foo.Bar();
}
mock.Verify();
}
interface Foo
{
void Bar();
}
- as you can see, #1 is accomplished by creating a new instance of
RemotingMock and passing in the type to mock (done in a similar fashion to
working with DynamicMock).
- for #2, create an instance of a MockServer (in a using block). the mock
server will install your remotable mock on the specified channel and uri.
- next, you can retrieve a proxy of your mock using RemotingServices and
invoke methods on it to your mocking-hearts' content.
simple, yet very powerful IMO!
here's another example from the CC.NET source:
[Test]
public void SendScheduleToCruiseManager()
{
CollectingConstraint projectNameConstraint = new
CollectingConstraint();
CollectingConstraint scheduleConstraint = new
CollectingConstraint();
RemotingMock cc = new RemotingMock(typeof
(ICruiseManager));
cc.Expect("Run", projectNameConstraint,
scheduleConstraint);
TcpChannel channel = new TcpChannel(2334);
using (MockServer server = new
MockServer(cc.MarshalByRefInstance, channel, "MockCruise.rem"))
{
Runner runner = new Runner();
runner.Url = "tcp://localhost:2334/MockCruise.rem";
runner.Run("myProject");
}
AssertEquals("myProject",
projectNameConstraint.Parameter);
AssertEquals(new Schedule(),
scheduleConstraint.Parameter);
cc.Verify();
}
thoughts? comments?
cheers,
owen.
---
R. Owen Rogers
ThoughtWorks Ltd
ThoughtWorks - Deliver with passion!
|