0

I am new for unit testing. I have created test method in unit testing project. In that test method I called API method for testing . API method connected with database. When i run unit test project getting "No connection found" error. Test method always getting failure.

Please suggest you solution. Please find my code

TestCase:

[TestMethod] 
public void ValidateCheckInCheckOut_Test2() 
{ 
    int task_id = 0; 
    var userToken = "dsdjs2"; 
    Object response = new Object (); 
    var actual = class.GetValidationResponse(task_id, userToken); Assert.AreNotEqual(actual.GetType(), response.GetType());
}

API method:

public void GetValidationResponse(int taskid , string userToken) 
{ 
    using(var context = new DbContext()) 
    { 
        Some logic......
    } 
}
4
  • You need to solve that error. Without seeing your code that's the only suggestion we can give. Why your unit test calls actual API? Also why unit tests connects to actual database? Commented Oct 4, 2017 at 5:18
  • Please find my code TestCase: [TestMethod] public void ValidateCheckInCheckOut_Test2() { int task_id = 0; var userToken = "dsdjs2"; Object response = new Object (); var actual = class.GetValidationResponse(task_id, userToken); Assert.AreNotEqual(actual.GetType(), response.GetType()); } API method: public void GetValidationResponse(int taskid , string userToken) { using(var context = new DbContext()) { Some logic...... } } Commented Oct 4, 2017 at 5:20
  • Update question with code you put in the comments Commented Oct 4, 2017 at 5:35
  • Yes upadated. Please suggest the solution. Commented Oct 4, 2017 at 5:40

1 Answer 1

1

As far as your unit tests are concerned, they should not be calling your actual API or your database rather the service calls should be mocked to get the desired result for the unit tests.

But for your question about no connection found, if you are using connection string for your API then you also need to add that connection string in App.config of the Unit test project to access the database through the connection string.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you please share code how are you accessing database connection string in API(not in unit test project)
public void GetValidationResponse(int taskid , string userToken) { using(var context = new newEntities()) { Some logic...... } }
You must be having the connection string for your database in Web.config, please copy paste that and use it in your unit test's App.config then you can write same code to access database in the unit test project as well.
you mean, have to write same logic code in unit test what i written in API. am i right?
No, if you will call GetValidationResponse method from your unit test project, it will run well. which in case you need to do from your unit tests.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.