473,389 Members | 1,322 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,389 software developers and data experts.

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 1554
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
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...
11
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...
0
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...
5
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...
2
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...
72
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: ...
1
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...
27
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...
24
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
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,...
0
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...
0
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...

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.