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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, the way I do it is as follows:
My Data Layer has an interface, such as follows:
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:
With this, i can now create a “fake” data service in my TDD project:
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.