473,516 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

testing question

In my solution, I seperate the code into one project and test into
another project (NUnit). Now, in one of my test class, I need to
reference a file. This file can not be created on the fly and it has
to be added to the test project and during the testing, I need to load
them from project file and use it for testing. This makes the testing
easy since there will be no external dependency. My question is, how
could I achive this? or is there a better way to do this?
Thanks.
Sep 13 '08 #1
3 1561
CSharper <cs******@gmx.comwrote:
In my solution, I seperate the code into one project and test into
another project (NUnit). Now, in one of my test class, I need to
reference a file. This file can not be created on the fly and it has
to be added to the test project and during the testing, I need to load
them from project file and use it for testing. This makes the testing
easy since there will be no external dependency. My question is, how
could I achive this? or is there a better way to do this?
Do you need it to reference an actual file, or just the data? I've
always found that adding files to the assembly and then calling
Assembly.GetResourceAsStream is a good way of working. You don't get
hundreds of files cluttering up the output directory, but you can still
work on them sensibly in Visual Studio.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 13 '08 #2
The constructor of your class could take an IServiceProvider instance. When
it needs the file contents you would ask it for a specific interface

public interface IFileContentProvider
{
byte[] GetFileContent(string fileName);
}

So you can do something like this

IFileContentProvider fcp =
(IFileContentProvider)seviceProvider.GetService(ty peof(IFileContentProvider));
byte[] data = fcp.GetFileContent("c:\\temp\\data.txt");
In the test you can use a mocking framework (I use Rhino Mocks) to do this

MockRepository mocks = new MockRepository();
mocks.RecordAll();

IFileContentProvider mockfcp = mocks.StrictMock<IFileContentProvider>();

IServiceProvider mockServiceProvider = mock.StrictMock<IServiceProvider>();
Expect.Call(mockServiceProvider.GetService(typeof( IFileContentProvider)).Return(mockfcp);

byte[] result = ( some pre-defined test data here )
Expect.Call(mockfcp.GetFileContent("c:\\temp\\data .txt")).Return(result);

mocks.ReplayAll();

YourClass yc = new YourClass(mockServiceProvider);
yc.DoSomething();

mocks.VerifyAll();

Pete

Sep 14 '08 #3
On Sep 14, 9:18*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
The constructor of your class could take an IServiceProvider instance. *When
it needs the file contents you would ask it for a specific interface

public interface IFileContentProvider
{
* * byte[] GetFileContent(string fileName);

}

So you can do something like this

IFileContentProvider fcp =
(IFileContentProvider)seviceProvider.GetService(ty peof(IFileContentProvider ));
byte[] data = fcp.GetFileContent("c:\\temp\\data.txt");

In the test you can use a mocking framework (I use Rhino Mocks) to do this

MockRepository mocks = new MockRepository();
mocks.RecordAll();

IFileContentProvider mockfcp = mocks.StrictMock<IFileContentProvider>();

IServiceProvider mockServiceProvider = mock.StrictMock<IServiceProvider>();
Expect.Call(mockServiceProvider.GetService(typeof( IFileContentProvider)).Re turn(mockfcp);

byte[] result = ( some pre-defined test data here )
Expect.Call(mockfcp.GetFileContent("c:\\temp\\data .txt")).Return(result);

mocks.ReplayAll();

YourClass yc = new YourClass(mockServiceProvider);
yc.DoSomething();

mocks.VerifyAll();

Pete
Thank you both for the great answer.
Sep 15 '08 #4

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

Similar topics

4
3723
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is possible). Most of my programming these days involves using PHP for creating script files for automating tasks and procedures (locally), and also for...
11
2304
by: rhat | last post by:
Hi Everyone, I've recently been reading some articles about unit-testing in Python , but I am a bit confused: where do I go to get started with this? I tried googling for "unittest" but all I've found are some old links to projects that already use it, and the older (as the articles put it) PyUnit project. I'm sorry if this is a obvious...
0
1722
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use this method? If so, what the heck is it called? Do you think it would work in other projects, or did we just luck out? Jonathan Allen ...
5
2849
by: Pete | last post by:
Hi folks, I've been looking at unit testing frameworks (csUnit, NUnit, etc) and have gathered some info on them (see the older thread on c# 2004). My question is this: how do they work with windows forms? All the samples I've seen are for trivial testing, such as making sure a property sets it's value correctly. How would I go about...
2
3206
by: Naveen Mukkelli | last post by:
Hi, I'm writing a client/server application using C#. The server accepts connecitons asynchronously, using BeginAccept/EndAccept. Is there any way we can write some unit tests(NUnit) to test the behaviour of accepting connections and testing some other private methods that would be called when the server receives a connection request.
72
5159
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
1
2132
by: google1 | last post by:
Has anyone written this one? Seen such a thing? Please send me a link: Skill Testing Question Exploder --------------------------------------------- Basically the plan is to create the javascript powered form one can surf to to bypass cheating via Window calculator for Skill Testing Question tests. One simply copys the Skill Testing...
27
2518
by: brad | last post by:
Does anyone else feel that unittesting is too much work? Not in general, just the official unittest module for small to medium sized projects? It seems easier to write some quick methods that are used when needed rather than building a program with integrated unittesting. I see the value of it (the official unittest that is)... especially...
24
2489
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do you ensure that the code will work correctly in the future? Short version:
0
7273
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...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
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. ...
0
7574
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...
1
7136
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...
0
7547
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...
0
4769
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...
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.