471,353 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 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 1479
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hugh Cowan | last post: by
11 posts views Thread by rhat | last post: by
reply views Thread by Jonathan Allen | last post: by
5 posts views Thread by Pete | last post: by
2 posts views Thread by Naveen Mukkelli | last post: by
72 posts views Thread by Jacob | last post: by
1 post views Thread by google1 | last post: by
27 posts views Thread by brad | last post: by
24 posts views Thread by David | last post: by

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.