views:

85

answers:

1

How do I unit test my code that has LTS Datacontext. I get error while testing, I have a lot of Datacontexts and manually adding the Connection string is a pain, any suggestions.

+1  A: 

Well, the way I do it is as follows:

My Data Layer has an interface, such as follows:

public class MyDataLayer : iMyDataLayer
{
  public string GetMyData(parameters)
  {
    return myQueryValue;
  }

}

public interface iMyDataLayer
{
  public string GetMyData(parameters);
}

Now, in my constructor for my main code base (business logic) I will have a parameter to pass in the interface for the data layer:

private iMyDataLayer DataLayer;

public class MyBusinessLogic(iMyDataLayer dataLayer)
{
   DataLayer = dayaLayer
}

public string GetMyData(parameters)
{
  return DataLayer.GetMyData(parameters)
}

With this, i can now create a "fake" data service in my TDD project:

public class FakeDataLayer : iMyDataLayer
{
      public string GetMyData(parameters)
      {
        return "Some Default Value or Object";
      }

}

So now when i run my test, I can now pass in my fake data layer object to my business logic, from here it will invoke the fake logic and get back a default result.

Now, granted you won't be working with real data here. However, if you setup fake objects with real valid/invalid data, you can test your business logic this way without having to connect to a database.

Hope this helps. Let me know if you need clarification on anything.

Jason Heine
Where do I hook up my datacontexts in the test.
Greens
Thanks very much.
Greens
You won't have data contexts because you can use the Fake service (which would normally have your data contexts in there to return a static result)
Jason Heine