EmberJS 測(cè)試控制器操作

2018-01-04 14:21 更新

描述

它使用一些計(jì)算的屬性和定義的操作來(lái)測(cè)試控制器。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Controller Actions</title>
         <!-- CDN's -->
         <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://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>
   </head>
<body>
   <div id="qunit"></div>
   <div id="ember-testing"></div>

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

      //'Ember.ArrayController' provides a way to publish a collection of objects which has some computed properties
      App.PostsController = Ember.ArrayController.extend({
         firstname: 'Jhon',
         lastname: 'Smith',

         setLastname: function(str) {
            this.set('lastname', str);
         },
         actions: {
            //'fullname' sets a property on the controller and also calls a method
            fullname: function(str) {
               this.set('firstname', 'Manu');
               this.setLastname(str);
            }
         }
      });

      //These methods are used to prepare the application for testing
      //emq.globalize();
      App.setupForTesting();
      App.injectTestHelpers();

      //The 'DefaultResolver' defines the default lookup rules before consulting the container for registered items
      setResolver(Ember.DefaultResolver.create({ namespace: App }));

      //The 'moduleFor' helper is used to setup a test container
      moduleFor('controller:posts', 'Posts Controller');
      test('calling the action fullname updates firstname and lastname', function() {

         //'this.subject()' is used to get an instance of the 'PostsController'
         var ctrl = this.subject();

         // Here, checking the properties before the action is triggered
         equal(ctrl.get('firstname'), 'First Name ');
         equal(ctrl.get('lastname'), 'Last Name ');

         //Trigger the action on the controller by using the 'send' method
         ctrl.send('fullname ', 'Tested....');

         //Assert the values that have been updated by triggering an action.
         equal(ctrl.get('firstname'), 'Tested');
         equal(ctrl.get('lastname'), 'Tested');
      });
   </script>
</body>
</html>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在testing_cntrlActions.html文件中

  • 在瀏覽器中打開此HTML文件。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)