Prepare the test environment
Unit testing usually consists of three parts:
Arrange-prepare or set boundary conditions for testing
Act-perform operations
Assert-verify whether the result is expected
We use nunit as our testing framework. First, we introduce a base class specificationbase to allow us to write our unit tests in a more natural way. SpecificationbaseCodeAs follows:
Public abstract class Specificationbase {[ Testfixturesetup ] Public void Testfixturesetup () {beforealltests ();}[ Setup ] Public void Setup () {given (); When ();}[ Teardown ] Public void Teardown () {cleanup ();}[ Testfixtureteardown ] Public void Testfixtureteardown () {afteralltests ();} Protected virtual void Beforealltests (){} Protected virtual void Given (){} Protected virtual void When (){} Protected virtual void Cleanup (){} Protected virtual void Afteralltests (){}}
The preceding class uses four important features of nunit:
Testfixturesetup-Call before running all test functions in the current test class
Testfixtureteardown-Call after all the test functions of the current test class are completed
Setup-Called before each test function of the current test class is run
Teardown-Called after each test function of the current test class is run
For details about how to use nunit, refer to this article.Article: Detailed usage of nunit.
In our base class, all four methods call virtual methods. The benefit of this implementation is that its subclass will no longer use processing features.
Next we will add a then feature, which is defined as a sub-feature of testattribute. The Code is as follows:
Public classThenattribute:Testattribute{}
With this then feature, we can use the given-when-then syntax in our test.
To represent the usage of the previous base class, we compile a unit test to verify the property of the name value object. The Code is as follows:
Public class When_creating_a_name_value_object : Specificationbase { Private string Lastname; Private string Firstname; Private string Middlename; Private Name Result; Protected override void Given () {lastname = "Schenker" ; Firstname = "Gabriel" ; Middlename = "N ." ;} Protected override void When () {result = New Name (Lastname, firstname, middlename );}[ Then ] Public void It_should_populate_the_last_name_property (){ Assert . Equals (result. lastname, lastname );}[ Then ] Public void It_should_populate_the_first_name_property (){ Assert . Equals (result. firstname, firstname );}[ Then ] Public void It_should_populate_the_middle_name_property (){Assert . Equals (result. middlename, middlename );}}
Test ing
To check whether the database architecture is correct and whether an entity is correctly mapped to the underlying database architecture, we must perform some steps. The first step is to create an object and initialize the value. The Code is as follows:
VaR Product =NewProduct{Name ="Apple", Description ="DescriptionForApple", Unitprice = 1.50 m, reorderlevel = 10, discontinued =True,};
Then we must save the product, as shown in the following code:
Session. Save (product); Session. Flush ();
Session. Flush () is used to forcibly write all the modifications to the database. In our example, a new product record is used.
If a new database record is successfully created, we want to know if all the attributes have been written to the database as defined. To achieve this, we must load the product just written into the database. The Code is as follows:
Session. evict (product); var fromdb = session. Get <Product> (Product. ID );
Session. evict (product) is used to remove objects stored in the level-1 cache to ensure that the test data comes from the database rather than the level-1 cache.
In the assert section, make sure that the product is stored in the database. If the object is not stored in the database, nhib.pdf returns NULL when loading the object. The Code is as follows:
Assert. That (fromdb, is. Not. null );
After you know that the data is stored in the database, you need to compare the property value with the original product object. The Code is as follows:
Assert. that (fromdb. name, is. similar to (product. name); assert. that (fromdb. description, is. similar to (product. description); assert. that (fromdb. unitprice, is. similar to (product. unitprice); assert. that (fromdb. reorderlevel, is. similar to (product. reorderlevel); assert. that (fromdb. discontinued, is. similar to (product. discontinued ));
If this test passes, we can ensure that:
1. The database architecture of the product entity is correct.
2. The ing definition of the product object is correct.
Test the ing using FLUENT nhib.pdf
Fluent nhiheiProgramThe set contains a persistencespecification <t> class, which can be used to quickly define the test. The code for the product entity test is as follows:
NewPersistencespecification<Product> (Session). checkproperty (x => X. Name,"Apple"). Checkproperty (x => X. description,"DescriptionForApple"). Checkproperty (x => X. unitprice, 1.50 m). checkproperty (x => X. reorderlevel, 10). checkproperty (x => X. discontinued,True). Verifythemappings ();
For each property we want to test, we call the checkproperty method. The verifythemappings method must be called at the end of the test.
Create a basic test
In this example, the basic framework is implemented for our ing test.
1. Open the orderingsystem used in method 1 of ing model to database in nhib.pdf beginner's Guide (5.
2. Add a class library project: orderingsystem. Tests in the solution.
3. Add references to the Nhibernate. dll, Nhibernate. bytecode. Castle. dll, and fluentnhibernate. dll assembly for the orderingsystem. Tests project.
4. Make sure that the orderingsystem. Project references the two sets nhib.pdf. dll and fluentnhib.pdf. dll. If not, reference them.
5. Download nunit, decompress it, and add a reference to nunit. Framework. DLL for orderingsystem. Tests.
6. Add a class thenattribute to orderingsystem. Tests to inherit from the nunit testattribute class, as shown in the following code:
Public classThenattribute:Testattribute{}
7. Add a class in orderingsystem. tests: specificationbase, which is the same as the previous specificationbase class code.
8. Add another abstract class: mappingspecificationbase, which inherits from specificationbase. The Code is as follows:
Public abstract class Mappingspecificationbase : Specificationbase { Protected Configuration Configuration; Private Isessionfactory Sessionfactory; Protected Isession Session; Protected override void Beforealltests () {configuration = Fluently . Configure (). Database (definedatabase). mappings (definemappings). buildconfiguration (); createschema (configuration); sessionfactory = configuration. buildsessionfactory ();} Protected Isession Opensession (){ Return Sessionfactory. opensession ();} Protected override void Given (){ Base . Given (); Session = opensession ();} Protected Abstract Ipersistenceconfigurer Definedatabase (); Protected abstract void Definemappings (Mappingconfiguration M ); Protected virtual void Createschema ( Configuration CFG ){}}
9. Add a class: entity_mapping_spec to orderingsystem. Tests, inherit from mappingspecificationbase, and use the [testfixture] feature to modify the class, as shown in the following code:
[Testfixture]Public classEntity_mapping_spec:Mappingspecificationbase{}
10. Rewrite the definedatabase method. The Code is as follows:
Protected overrideIpersistenceconfigurerDefinedatabase (){ReturnMssqlconfiguration. Mssql2008. connectionstring ("Server = .;"+"Database = nh3beginnersguide ;"+"Integrated Security = sspi ;");}
11. Rewrite the definemappings method and add the following code:
Protected override voidDefinemappings (MappingconfigurationM) {M. fluentmappings. addfromassemblyof <Productmap> ();}
12. Rewrite the createschema method and add the following code:
Protected override voidCreateschema (nhib.pdf. cfg.ConfigurationCFG ){NewSchemaexport(CFG). Execute (False,True,False);}
13. Add a method to test whether the product entity ing is correct. The Code is as follows:
[Then]Public voidIt_should_correctly_map_a_product (){NewPersistencespecification<Product> (Session). checkproperty (x => X. Name,"Apple"). Checkproperty (x => X. description,"C"). Checkproperty (x => X. unitprice, 1.50 m). checkproperty (x => X. reorderlevel, 10). checkproperty (x => X. discontinued,True). Verifythemappings ();}
14. running the test now throws the nhib.pdf. invalidproxytypeexception exception. The reason is that if the object class defines a constructor with parameters, you must explicitly define a constructor without parameters. In addition, the virtual keyword must be added to the methods defined in the class. For more information, see this article on several minor issues in the nhib.pdf test.
15. After modification, the operation is successful, as shown in:
Use SQLite in testing
1. Complete the preceding code.
2. Add usingsqlite to the orderingsystem. Tests project.
3. Add a reference to the system. Data. SQLite. dll assembly for orderingsystem. Tests and set its copy local attribute to true.
4. Add a class in the usingsqlite Folder: entity_mapping_spec_for_sqlite, inherit from mappingspecificationbase and use testfixture to modify the class. The Code is as follows:
[Testfixture]Public classEntity_mapping_spec_for_sqlite:Mappingspecificationbase{}
5. Rewrite the definedatabase method. The Code is as follows:
Protected overrideIpersistenceconfigurerDefinedatabase (){ReturnSqliteconfiguration. Standard. inmemory (). showsql ();}
6. Rewrite the definemappings method. The Code is as follows:
Protected override voidDefinemappings (MappingconfigurationM) {M. fluentmappings. addfromassemblyof <Productmap> ();}
7. Rewrite the given method. The Code is as follows:
Protected override voidGiven (){Base. Given ();NewSchemaexport(Configuration). Execute (False,True,False, Session. connection,Null);}
8. The test code for adding product entity ing is as follows:
[Then]Public voidIt_should_correctly_map_a_product (){NewPersistencespecification<Product> (Session). checkproperty (x => X. Name,"Apple"). Checkproperty (x => X. description,"Some description"). Checkproperty (x => X. unitprice, 1.50 m). checkproperty (x => X. reorderlevel, 10). checkproperty (x => X. discontinued,True). Verifythemappings ();}
9. Run the test, as shown in: