2013-09-30 6 views
18

Я хочу написать тесты JS. Производственный код написан с RequireJS. я нашел тест LIB имени Squire.js: https://github.com/iammerrick/Squire.js/Пример Squire.js с Jasmine и RequireJS

с сайтом Squire.js

Run генерирует функцию, которая будет получать сделано обратный вызов и выполнить его после того, как тестовая функция будет завершена. Особенно полезно для фреймворков, где асинхронность обрабатывается с помощью обратного вызова. Вот пример с Mocha.js. Жасмин может предложить этот подход обратного вызова с использованием Jasmin.Async.»

Я не знаю, как использовать это с Жасмин асинхронной. Маленький пример был бы очень полезен.

ответ

21

Это хорошая настройка для вставки . модули с издевался зависимостей в тестах Это встык пример, чтобы кто-то начал, пропустить до конца, если вы просто хотите, чтобы увидеть спецификации Структура

Папка:.

Jasmine 
|-lib 
||-jasmine   -- Contains all the Jasmine files 
|||-boot.js 
|||-jasmine-html.js 
|||-jasmine.js 
|||-jasmine.css 
|||-jasmine_favicon.png 
|||-... 
|-spec    -- Spec files go here 
||-hello-spec.js 
|SpecRunner.html 
|SpecRunner.js 
|squire.js 
Scripts 
|knockout.js 
|require.js 
|jquery.js 
App 
|-hello.js 
|-foo.js 

Во время вашего вопроса, Jasmine 1.3 была последней версией. С тех пор 2.0 был выпущен и содержит некоторые асинхронные улучшения. Обе версии включены здесь, 1.3 закомментирован.

SpecRunner.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title>Hello Spec Runner</title> 

    <link rel="shortcut icon" type="image/png" href="lib/jasmine/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="lib/jasmine/jasmine.css"> 
</head> 
<body> 
    <!-- Load RequireJS & the testsuite --> 
    <script src="../Scripts/require.js" data-main="SpecRunner.js" type="text/javascript" ></script> 
</body> 
</html> 

SpecRunner.js:

Этот вопрос Does Jasmine 2.0 really not work with require.js? был полезен в получении этого и работает.

(function() { 
    'use strict'; 

    // Configure RequireJS to shim Jasmine 
    requirejs.config({ 
     baseUrl: "../App", 
     paths: { 
      'jasmine' : '../Jasmine/lib/jasmine/jasmine', 
      'jasmine-html': '../Jasmine/lib/jasmine/jasmine-html', 
      'boot': '../Jasmine/lib/jasmine/boot', // This is not present in Jasmine 1.3 
      'spec' : '../Jasmine/spec', 
      'squire': '../Jasmine/squire', 
      'knockout': '../Scripts/knockout-2.3.0', 
      'jquery': '../Scripts/jquery-1.10.2' // This only used in the Jasmine 1.3 case. 
     }, 
     shim: { 
      'jasmine': { 
       exports: 'jasmine' 
      }, 
      'jasmine-html': { 
       deps: ['jasmine'], 
       exports: 'jasmine' 
      }, 
      'boot': { 
       deps: ['jasmine', 'jasmine-html'], 
       exports: 'jasmine' 
      }, 
      "squire": { 
       exports: "squire" 
      } 
     } 
    }); 

    // Define all of your specs here. These are RequireJS modules. 
    var specs = [ 
     'spec/hello-spec' 
    ]; 

    // Load Jasmine - This will still create all of the normal Jasmine browser globals unless `boot.js` is re-written to use the 
    // AMD or UMD specs. `boot.js` will do a bunch of configuration and attach it's initializers to `window.onload()`. Because 
    // we are using RequireJS `window.onload()` has already been triggered so we have to manually call it again. This will 
    // initialize the HTML Reporter and execute the environment. 
    require(['boot'], function() { 

     // Load the specs 
     require(specs, function() { 

      // Initialize the HTML Reporter and execute the environment (setup by `boot.js`) 
      window.onload(); 
     }); 
    }); 

    /****** 
    * Use this require if you're on Jasmine 1.3 
    ******/ 
    //require(['jquery', 'jasmine-html'], function ($, jasmine) { 
    // var jasmineEnv = jasmine.getEnv(); 
    // jasmineEnv.updateInterval = 1000; 

    // var htmlReporter = new jasmine.HtmlReporter(); 

    // jasmineEnv.addReporter(htmlReporter); 

    // jasmineEnv.specFilter = function(spec) { 
    //  return htmlReporter.specFilter(spec); 
    // }; 

    // $(function() { 
    //  require(specs, function(spec) { 
    //   jasmineEnv.execute(); 
    //  }); 
    // }); 
    //}); 

    // end 1.3 
})(); 

foo.js:

define(['knockout'], function (ko) { 

    var message = ko.observable("Go away, World!"); 

    return { 
     message: message() 
    }; 
}); 

hello.js:

define(['foo'], function (foo) { 

    return { 
     message : foo.message 
    }; 

}); 

HELLO-spec.js:

define(['squire'], function (Squire) { 

    var injector = new Squire(); 
    var builder = injector 
     .mock('foo', { 
       message: "Hello, World!" 
     }); 

    describe('hello', function() { 
     var hello; 

     beforeEach(function (done) { 
      // For async: 
      // accept a "done" parameter and Jasmine will wait... 
      builder.require(['hello'], function (hi) { 
       hello = hi; 
       done(); // ...until you invoke it. 
      }); 

      /******* 
      * Use the following if you're on 1.3 
      *********/ 
      //var done; 
      //runs(function() { 
      // builder.require(['hello'], function (hi) { 
      //  hello = hi; 
      //  done = true; 
      // }); 
      //}); 

      //waitsFor(function() { 
      // return done; 
      //}, "Unable to load dependency not loaded", 750); 

      // end 1.3 
     }); 

     it('is welcoming', function() { 
      expect(hello.message).toBe("Hello, World!"); 
     }); 
    }); 

    describe('hello no mock foo', function() { 
     var hello; 

     beforeEach(function (done) { 
      // For async: 
      // accept a "done" parameter and Jasmine will wait... 
      require(['hello'], function (hi) { 
       hello = hi; 
       done(); // ...until you invoke it. 
      }); 

      /******* 
      * Use the following if you're on 1.3 
      *********/ 
      //var done; 
      //runs(function() { 
      // require(['hello'], function (hi) { 
      //  hello = hi; 
      //  done = true; 
      // }); 
      //}); 

      //waitsFor(function() { 
      // return done; 
      //}, "Unable to load dependency not loaded", 750); 

      // end 1.3 
     }); 

     it('is less than welcoming', function() { 
      expect(hello.message).toBe("Go away, World!"); 
     }); 
    }); 
}); 

Детали

Линии

var injector = new Squire(); 
var builder = injector 
    .mock('foo', { 
     message: "Hello, World!" 
    }); 

зависимость макете Hello на Foo, а затем

builder.require(['hello'], function (hi) { 
    hello = hi; 
    done(); // ...until you invoke it. 
}); 

загружает модуль привет с помощью высмеивал Foo. Сравните с использованием требуют себя, чтобы загрузить привет во втором тесте:

require(['hello'], function (hi) { 
    hello = hi; 
    done(); // ...until you invoke it. 
}); 

жасмин документы http://jasmine.github.io/ может заполнить Вас на «Done()» функциональности (2.0) или «запускает/waitsFor» (1.3).

 Смежные вопросы

  • Нет связанных вопросов^_^