EmberJS 测试路由

2018-01-03 17:03 更新

测试路由

您可以使用两种方法测试路由。一个是接受,另一个是单元测试。验收测试通常用于测试应用程序的工作流,单元测试通常用于测试一小段代码。

例子

<!DOCTYPE html>
   <html>
      <head>
         <title>EmberJs Tesing Routes</title>
         <!-- CSS for testing programs -->
         <link href="https://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
         <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
         <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
         <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script>
         <script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script>
         <script src="https://rawgit.com/rwjblue/ember-qunit-builds/master/ember-qunit.js"></script>
         <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
         <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
     </head>
     <body>
         <div id="qunit"> </div>
         <div id="ember-testing"></div>

         <script type="text/x-handlebars">
            {{#link-to 'index'}}Index{{/link-to}}
            {{#link-to 'home'}}Home{{/link-to}}
            {{outlet}}
         </script>

         <script type="text/x-handlebars" data-template-name="index">
            <h2>Index</h2>
            <button {{action "displayAlert" navtext}}>Index </button>
         </script>

         <script type="text/x-handlebars" data-template-name="home">
             <h2>Home</h2>
             <button {{action "displayAlert" navtext}}>Home </button>
         </script>

         <script type="text/javascript">
            //Creates an instance of Ember.Application and assign it to a global variable
            App = Ember.Application.create();

            //The 'map' method can be invoked to define URL mappings
           App.Router.map(function() {
             this.route('home');
           });

           //Customize the behavior of a route by creating an Ember.Route subclass
          App.ApplicationRoute = Ember.Route.extend({
              _displayText: function(navtext) {
                  document.write(navtext);
              },
              actions: {
                 displayText: function(navtext) {
                    this._displayText(navtext);
                  }
              }
          });

          //The IndexController template stores its properties and sends its actions to the Controller
          App.IndexController = Ember.Controller.extend({
             navtext: 'Index'
          });

          //The HomeController template stores its properties and sends its actions to the Controller
          App.HomeController = Ember.Controller.extend({
             navtext: 'Home'
          });

          //emq.globalize();

          //The 'DefaultResolver' defines the default lookup rules before consulting the container for registered items
          setResolver(Ember.DefaultResolver.create({ namespace: App }));
          App.setupForTesting();   //This method is used to prepare the application for testing

          var res;

          //The 'moduleFor' helper is used to setup a test container
          moduleFor('route:application', 'Unit: route/application', {
             setup: function() {
                res = window.document.write;   //Store a reference to the browser
             },
            teardown: function() {
              window.document.write = res;   //Restore original functions
            }
          });

          test('Display is called on displayText', function() {

          //'this.subject()' is used to get an instance of the 'IndexController' and 'HomeController'
          var route = this.subject();

          //The text to be display
          var expectedText = 'Tutorialspoint';

          //Defines window.document.write to perform a qunit test
          window.document.write = function(navtext) {
             equal(navtext, expectedText, 'expected ' + navtext + ' to ' + expectedText);
          }

          //Call the _displayText function which triggers the qunit test above
          route._displayText(expectedText);
        });
    </script>
    </body>
</html>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上面的代码保存在testing_routes.html文件中

  • 在浏览器中打开此HTML文件。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号