views:

42

answers:

1

I'm trying to use RhinoMocks to stub out a third party component. The third party component looks like the following.

public class connection
{
    public connection(string host,int port)
    {}

    public void Submit(message msg)
    {}
}

public class message
{
    public message(string recipient)
    {}
{

When I attempt to use a stub it instead returns an actual instance of the object and my message will actually be sent if I use a valid host and port. If I don't use a valid host or port the constructor on the connection object throws an exception. I don't want a real object I just want a stub. What am I missing? Below is my code.

        Connection con = MockRepository.GenerateStub<Connection>("host", 25);
        Message msg = new Message("[email protected]");
        msg.AddRecipient(new Recipient("[email protected]"));
        con.Submit(msg);
+1  A: 

Hi Dillon

I know you cannot mock a class with Rhino that isn't virtual (ie, an interface) and I would think the same applies to stubs (not sure though).

Can you inherit from the 3rd party class? If so you can make your own stub easily enough.

If you post enough code to get to your actual assertion (I'm guessing it's to see that the intended Recipient got the msg) I'm sure someone here can help you get a useful test one way or another.

Berryl

Berryl
It wasn't virtual. Didn't check my assumptions. Doh. Wrapped it in class so I was able to mock it out.
Dillon
I just got the same problem, I wish there was a more explicit error thrown...
Paulo Manuel Santos