unit test...
There is a good article on TheServerside.Net, "10 Ways to make your code more testable" (10 ways to make your code more measurable), there are several ways to make the code more With a functionable method, it is also a basic Principla that must be followed when writing code, but there are also a few very interesting:
As long as it is feasible, let the method return a value. This can not only make the code easier to be tested, but also easier to let the caller understand its operating state. However, the author also said that this method is not "not this," but "as much as possible".
For operations involving the database, the solution proposed by this article is a very common Mock Data Access Object, that is, using one of the fake data access objects that do not really read and write the database, to simulate real DAO. behavior.
It is said that this is indeed a problem that is easily scratched in the unit test. Here I solemnly recommend Mbunit tools, and it has two most common methods for solving this problem, making our days easier.
1, ROLLBACKATTRIBUTE, this feature takes advantage of COM TRANSACTION, puts the operation of the entire test method in a transaction to reach the purpose of automatic rollback.
[TEST] [ROLLBACK] PUBLIC VOID TESTMETHOD () Roy Osherove There is a detailed detailed description of this method.
2, completely restore the database, this method is based on a very simple data recovery method: in a place (usually before or after the test start), let the database automatically restore the BACKUP files we provide.
[TestFixTure] [SQLRESTOREINFO ("Connectionstring", "DatabaseName", @ "c: /db.bak"] public class testclass {
[TEST] [RestoreDatabaseFirst] public void testmethod ()
The "SqlrestoreInfo" feature provides the necessary connection string, database name, and database backup file information, "RestoreDatabaseFirst" features indicate that the database is used to restore the database before performing test methods. Finally, introduce a series of articles, Automating Unit Testing With a base class posts, explains a method of "automated" testing.