QUnit - İç İçe Modüller

Gruplanmış test işlevlerine sahip modüller, yuvalanmış modülleri tanımlamak için kullanılır. QUnit, ilk olarak bildirilmiş olsalar bile, iç içe geçmiş modüllerde derinlemesine gitmeden önce üst modül üzerinde testler çalıştırın. beforeEach ve afterEachYuvalanmış bir modül çağrısındaki geri çağırmalar, LIFO (Son Giren, İlk Çıkar) Modunda üst kancalara yığılır. Bağımsız değişken ve kancaları kullanarak her testten önce ve sonra çalıştırılacak kodu belirtebilirsiniz.

Hook'lar ayrıca her testin bağlamında paylaşılacak özellikler oluşturmak için de kullanılabilir. Hooks nesnesindeki herhangi bir ek özellik bu bağlama eklenecektir. Geri arama bağımsız değişkeniyle QUnit.module'ü çağırırsanız, hooks bağımsız değişkeni isteğe bağlıdır.

Modülün geri çağrısı, ortamın özellikleri modülün testlerine, kancalarına ve iç içe modüllere kopyalanmış olarak test ortamı olarak içeriğe sahip olarak çağrılır.

<html>
   <head>
      <meta charset = "utf-8">
      <title>QUnit basic example</title>
      <link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css">
      <script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script>
   </head>
   
   <body>
      <div id = "qunit"></div>
      <div id = "qunit-fixture"></div> 
      <script>
         QUnit.module( "parent module", function( hooks ) {
            hooks.beforeEach( function( assert ) {
               assert.ok( true, "beforeEach called" );
            });

            hooks.afterEach( function( assert ) {
               assert.ok( true, "afterEach called" );
            });

            QUnit.test( "hook test 1", function( assert ) {
               assert.expect( 2 );
            });

            QUnit.module( "nested hook module", function( hooks ) {
               // This will run after the parent module's beforeEach hook
               hooks.beforeEach( function( assert ) {
                  assert.ok( true, "nested beforeEach called" );
               });

               // This will run before the parent module's afterEach
               hooks.afterEach( function( assert ) {
                  assert.ok( true, "nested afterEach called" );
               });

               QUnit.test( "hook test 2", function( assert ) {
                  assert.expect( 4 );
               });
            });
         });
      </script>

      <div id = "console" ></div>
   </body>
</html>

Çıkışı Doğrulayın

Aşağıdaki sonucu görmelisiniz -