How to mock Stream.Read method

  • Thread starter Thread starter Pawel Pabich
  • Start date Start date
P

Pawel Pabich

Hi,

I need to mock Stream.Read method with RhinoMocks but it looks like
it's impossible.
Any ideas?
 
Pawel Pabich said:
I need to mock Stream.Read method with RhinoMocks but it looks like
it's impossible.
Any ideas?

I believe it's possible to mock it in EasyMock.NET because Stream
derives from MarshalByRefObject. I haven't tried doing it in
RhinoMocks.

Do you *definitely* need a mock here rather than a stub though? Are you
testing that something works even in the face of Read not returning all
the data in one go, or something like that?

You could always create your own Stream class, rather than using
RhinoMocks.
 
The problem is that int Read(byte[] buffer,int offset, int size)
method does not return an array of bytes but it takes an empty array
of bytes as a parameter and then fills it up. I can not see any way
how to mock this method and set an expectation that let's say it
returns new byte[]{1,2,3}, If it returned an array of bytes there
would be no problem. I can always create my own MyStream class but
that's the work RihnoMocks is responsible for :)

thanks

Pawel
 
Pawel Pabich said:
The problem is that int Read(byte[] buffer,int offset, int size)
method does not return an array of bytes but it takes an empty array
of bytes as a parameter and then fills it up. I can not see any way
how to mock this method and set an expectation that let's say it
returns new byte[]{1,2,3}, If it returned an array of bytes there
would be no problem. I can always create my own MyStream class but
that's the work RihnoMocks is responsible for :)

You'd need to set up a custom action to execute when the method is
called. I'm sure RhinoMocks can do this, but I can't remember exactly
how off the top of my head. EasyMock.NET can't do it with the version
on SF.NET, but I've got a version that can if you need it. (I'd look
further into RhinoMocks though. There must be a way of executing a
delegate on method call.)
 
Yes, you are right there is a way of invoking a delegate but this
still require me to write some code that
I would rather avoid but it seems there is no way to achieve that.

Pawel Pabich said:
The problem is that int Read(byte[] buffer,int offset, int size)
method does not return an array of bytes but it takes an empty array
of bytes as a parameter and then fills it up. I can not see any way
how to mock this method and set an expectation that let's say it
returns new byte[]{1,2,3}, If it returned an array of bytes there
would be no problem. I can always create my own MyStream class but
that's the work RihnoMocks is responsible for :)

You'd need to set up a custom action to execute when the method is
called. I'm sure RhinoMocks can do this, but I can't remember exactly
how off the top of my head. EasyMock.NET can't do it with the version
on SF.NET, but I've got a version that can if you need it. (I'd look
further into RhinoMocks though. There must be a way of executing a
delegate on method call.)
 
Pawel Pabich said:
Yes, you are right there is a way of invoking a delegate but this
still require me to write some code that
I would rather avoid but it seems there is no way to achieve that.

There's very little code to write - and you could always write it in
such a way that you only need to write it once, so you can pass in
different byte arrays to create different delegates for future use,
etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top