EmberJS 測(cè)試關(guān)系

2018-01-03 17:08 更新

測(cè)試關(guān)系

關(guān)系定義了模型如何相互關(guān)聯(lián)。對(duì)于關(guān)系,您需要測(cè)試關(guān)系聲明是否正確設(shè)置。

例子

<!DOCTYPE html>
   <html>
      <head>
         <title>EmberJs Tesing Relationships</title>
         <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/javascript">
      //Creates an instance of Ember.Application and assign it to a global variable
      App = Ember.Application.create();

      //Create a subclass of 'DS.Model' for 'Car' model in the application
      App.Car = DS.Model.extend();

      App.Model = DS.Model.extend({
         car: DS.belongsTo('car')   //Use 'DS.belongsTo' to declare a one-to-one relationship between two models
      });

      //emq.globalize();
      App.setupForTesting();   //This method is used to prepare the application for testing
      App.rootElement = '#ember-testing';   //Ember.js applications's root element

      //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
      moduleForModel('model', 'Model model', {
         needs: ['model:car']
      });

      test('car relationship', function() {
         var Model = this.store().modelFor('model');   //Get the records loaded into the store
         var relationship = Ember.get(Model, 'relationshipsByName').get('car');   //A map the keys which are the relationships of a model using  'relationshipsByName' property

          //Check the values before we modify the post
         equal(relationship.key, 'car');
         equal(relationship.kind, 'belongsTo');   //Defines the value which is belongs to a particular model
      });
   </script>
</body>
</html>

輸出

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

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

  • 在瀏覽器中打開(kāi)此HTML文件。

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)