它用于通過更新defined屬性來更改對象的某些內(nèi)部狀態(tài)。
<!DOCTYPE html> <html> <head> <title>Emberjs Testing Observers</title> <!-- CDN's --> <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({ rootElement: '#ember-testing' //Ember.js applications's root element }); //To define a new Ember class, call the extend() method on Ember.Object App.MyFunc = Ember.Object.extend({ name: 'John', //The 'testMethod' alters internal state of the object by updating the name property testMethod: function() { this.set('name', 'Smith'); } }); App.setupForTesting(); module('Emberjs'); //Here, it tests the workflow of an application test('calling testMethod updates name', function() { //'myfunc' is an instance of our class MyFunc var myfunc = App.MyFunc.create(); //Call the 'testMethod' method and assert that the internal state is correct as a result of the method call myfunc.testMethod(); equal(myfunc.get('name'), 'Smith'); }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在testing_obj_methods.html文件中
在瀏覽器中打開此HTML文件。
更多建議: