Using MBUnit to Validate Database Values

I need to check the class that returns the value depends on the values ​​from the database. I could just push the database in unit test, but these values ​​may change. Is there a standard solution for this?

+3
source share
1 answer

The standard answer is to reverse engineer your class so you can mock the dependency. This is usually done by entering your data source as an interface into your class.

eg. You may have a class that acts like below

class John 
{
     public John() { }
     public void Load()
     {
          // call to db in here e.g SQLCommand
     }                  
}

Download depends on SQLCommand, so you always need to call db for this

,

.

class John 
{    IDataSource _db;
     public John(IDataSource db) 
     {
        _db = db;
     }
     public void Load()
     {
        _db.Load("John"); // IDataSource can now be either SQL 
        //or hardcoded or what ever much easier to test
     }                  
}

, / , . , . , . . , .

+4

Source: https://habr.com/ru/post/1721140/


All Articles