473,802 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5410
Pawel Pabich <pa**********@g mail.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 MarshalByRefObj ect. 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.co m>
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.com wrote:
Pawel Pabich <pawel.pab...@g mail.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 MarshalByRefObj ect. 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**********@g mail.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.co m>
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.com wrote:
Pawel Pabich <pawel.pab...@g mail.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**********@g mail.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.co m>
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
1976
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, when we use BarUser in a particular way, BarUser first calls method Bar.xyzzy with args "foo" and bar=2, then method Bar.do_nothing. # first, construct a mock object by example: m = Mock()
4
1856
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 handler supports. So, my mock class has to have appropriate methods defined on it. To avoid the hassle of manually defining lots of mock classes, I wanted to have a base class MockHandler so that I could do class...
4
1489
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 and its Position is 0. Here's the code: .... For Each attachment As Attachment In Attachments Using FS As New FileStream(app_data & fileName, FileMode.Create)
2
6619
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 to create a buffer of a pre-defined length: (ConstBufferByteSize=10000000). How can I read a stream into a buffer or string without knowing the number of chars the stream will contain?
1
2253
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 the interface, and then spit out a version of that that is suited to testing.
0
2890
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 ?myResp.GetResponseStream() {System.Net.ConnectStream}
0
1076
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 Homepage & Documentation http://www.voidspace.org.uk/python/mock.html * mock.py (module only) <http://www.voidspace.org.uk/downloads/mock.py * mock-0.4.0.zip (module, tests and documentation) <http:// www.voidspace.org.uk/downloads/mock-0.4.0.zip
2
3243
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) != 0) { // Process the buffer }
4
4966
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 JDK dynamic proxy because protected methods are not in any interface but we can create MockInterface with mock method under control and call it from overridden protected method to achieve our goal. The problem I’m trying to resolve now is that my...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.