views:

20

answers:

2

So I want to test one of my Functions in my Web Project, but it's not actually connected to anything in the project yet (someone else is working on that part). The Function takes in an "ID" field, goes off and does some queries and gets some data, performs some calculations on it, and then writes a bunch of lines to a FileStream and returns that stream. I pretty much just want to test it by having it write the file to my own computer locally, and working with that file directory after the Function completes.

So my question is mainly:

1) How do I call this Function just for testing purposes so I can test all the queries/calculations/File writes, etc without it being connected to another part of the application just yet.

2) How can I change the 'Return fs' for the FileStream to write to my own computer locally to view the file that has been written.

Thanks guys!

A: 

You need to read up on Unit Testing as this solves your problem in so many ways - it would also introduce you to dependency injection and mocking, which would be a great way to handle your problem.

Here is an overview...

Set up your class so it accepts the data-access and file-writer in the constructor. You can then pass in mock or stub version of the data access and file writer so you don't physically need to connect to a database or write to the file system to test your code.

In the "real world" you pass in the genuine data access and file writer.

In "test world" you use something such as MOQ or Rhino Mocks to create a pretend version of the data access, this means you can predict what will come back from the data access every time you test as it isn't the real database, it's some data you have prepared. You can also create a pretend file-writer that doesn't actually need to write a real file.

You can then test your class in isolation.

Dependency Injection:

http://msdn.microsoft.com/en-us/magazine/cc163739.aspx

Moq

http://code.google.com/p/moq/

Sohnee
A: 

To make your function testable you need to isolate all your dependencies and replace them in your test with stubs mocks. You can achieve this by wrappers around the file system classes and making sure your data layers classes have interfaces. With this your code could look like:

public class Something 
{
  IDataProvider provider;
  IFileSystem fileSystem;
  public Something(IDataProvider provider,  IFileSystem fileSystem) 
  {
     this.provider = provider;
     this.fileSystem = fileSystem;
  }

   void DoThing(int id) 
   {
        // make database call to get data
        var data = provider.GetData(id);

        fileSystem.Write("someFilePath",data);
   }

}

With this you can write a test as such (in this casing using Moq like syntax):

void SomeTest() 
{
  var mockDataProvider = new Mock<IDataProvider>();
  var mockFileSystem = new Mock<IFileSystem>();
  var something = new Something(mockDataProvider.Object, mockFileSystem.Object);
  var data = "someData";
  mockDataProvider.Setup(x => x.GetData(5)).Return(data);

  DoThing(5);

  mockFileSystem.Verify(x => x.Write("someFilePath",data);


}
Matthew Manela