473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to mock Stream.Read method

Hi,

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

Feb 13 '07 #1
5 5380
Pawel Pabich <pa**********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 13 '07 #2
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

On Feb 13, 1:52 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Pawel Pabich <pawel.pab...@gmail.comwrote:
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 14 '07 #3
Pawel Pabich <pa**********@gmail.comwrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #4
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.

On Feb 14, 7:29 am, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Pawel Pabich <pawel.pab...@gmail.comwrote:
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.)

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 14 '07 #5
Pawel Pabich <pa**********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John J. Lee | last post by:
I had the bright idea a week or two ago to construct mock objects (for unit testing) "by example" by calling methods on a generic mock object. Say we have a class Bar, and we want to check that,...
4
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a...
4
by: Craig Buchanan | last post by:
I am trying to save a MemoryStream (the attachment's Data property) to disk using a FileStream. It seems that the MemoryStream's Read method is the issue. The MemoryStream's size is 6680 bytes...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
1
by: earthwormgaz | last post by:
Has anyone seen a tool for C++ that automatically creates mock classes? I found MockMaker for Java, but I wondered if there was something similar for us C++ developers. The idea is to read in...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
0
by: Fuzzyman | last post by:
Mock 0.4.0 has just been released, the first release in about ten months (but worth the wait). Mock is a simple library for testing: specifically for mocking, stubbing and patching. * Mock...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: In regards to reading a stream, I know we use the Read() method. One could implement reading a stream using a while-loop. while (stream.Read(buffer, 0, buffer.Length) !=...
4
intelrate
by: intelrate | last post by:
Here is a question. I wanted my JUnit tests to be more modular and found how to mock protected methods for that purpose. Briefly speaking, we cannot mock and control calls of protected method with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.