testing question


C

CSharper

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.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

CSharper said:
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.
 
P

Peter Morris

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(typeof(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
 
Ad

Advertisements

C

CSharper

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(typeof(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top