<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                和其它的框架一樣,C層同樣不是指揮官,它只是東指揮一下,西指揮一下,并不負責數據運算與邏輯處理。本教程我們的重點仍然是javaee,所以在此,我們更多的給出代碼供參考。 # 新建teacher模型 `$ yo angular:service teacher` 命令執行后,將我們新建如下兩個文件: ``` create app/scripts/services/teacher.js create test/spec/services/teacher.js ``` # 增加all方法 app/scripts/services/teacher.js ``` 'use strict'; /** * @ngdoc service * @name webAppApp.teacher * @description * # teacher * Service in the webAppApp. */ angular.module('webAppApp') .service('teacher', function() { // Public API here return { // 獲取考場編排信息 all: function(callback) { var teachers = [ { name: '張三', username: 'zhangsan', sex: 0, email: 'zhangsan@yunzhiclub.com' }, { name: '李四', username: 'lisi', sex: 1, email: 'lisi@yunzhiclub.com' } ]; callback(teachers); }, }; }); ``` 在這里,我們使用了回調函數。這也是js里常見的方法。在這里,我們必須使用回調函數,以保障后期順利的與后臺對接。 > 回調函數:[http://www.hmoore.net/kongrp/angularjsguide/181089](http://www.hmoore.net/kongrp/angularjsguide/181089) 在開啟grunt serve的前提后,當我們對文件保存后,grunt會自動為我們檢查代碼的輸出是否正確,是否規范等信息。 # 單元測試 我們以前說:我們在M層中,每新建一個方法,都需要進行單元測試。angularjs也不例外。 下面,讓們使用karma來開啟第一個單元測試。 yoman在建立teacher.js的同時,在test文件夾的相同位置,為我們建立好了對應的測試文件。 我們找到test/spec/services/teacher.js,并鍵入以下內容: ``` ... it('應該取出來所有的教師數據', function() { var teachers; teacher.all(function (data){ teachers = data; console.log('取出數據如下:'); console.log(teachers); // 檢測tachers的長度,如果為2,正確,則不報錯。 expect(teachers.length).toBe(2); // 檢測teachers的第一個元素的用戶名是否為zhangsan expect(teachers[0].username).toBe('zhangsan'); // 測試一個錯誤的結果,假設我們期待返回的數組長度為1 expect(teachers.length).toBe(1); }); }); ... ``` 增加代碼后: ``` 'use strict'; describe('Service: teacher', function() { // load the service's module beforeEach(module('webAppApp')); // instantiate service var teacher; beforeEach(inject(function(_teacher_) { teacher = _teacher_; })); it('should do something', function() { expect(!!teacher).toBe(true); }); it('應該取出來所有的教師數據', function() { var teachers; teacher.all(function (data){ teachers = data; console.log('取出數據如下:'); console.log(teachers); // 檢測tachers的長度,如果為2,正確,則不報錯。 expect(teachers.length).toBe(2); // 檢測teachers的第一個元素的用戶名是否為zhangsan expect(teachers[0].username).toBe('zhangsan'); // 測試一個錯誤的結果,假設我們期待返回的數組長度為1 expect(teachers.length).toBe(1); }); }); }); ``` 保存文件后,測試自動執行,結果: ``` LOG: '取出數據如下:' LOG: [Object{name: '張三', username: 'zhangsan', sex: 0, email: 'zhangsan@yunzhiclub.com'}, Object{name: '李四', username: 'lisi', sex: 1, email: 'lisi@yunzhiclub.com'}] PhantomJS 2.1.1 (Mac OS X 0.0.0) Service: teacher 應該取出來所有的教師數據 FAILED Expected 2 to be 1. test/spec/services/teacher.js:30:41 all@app/scripts/services/teacher.js:20:25 test/spec/services/teacher.js:20:20 loaded@http://localhost:8080/context.js:151:17 PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 5 of 5 (1 FAILED) (0 secs / 0.039 secPhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 5 of 5 (1 FAILED) (0.009 secs / 0.039 secs) Warning: Task "karma:unit" failed. Use --force to continue. Aborted due to warnings. ``` 我們看到,shell中,打印出了log信息。當取出的數組長度與我們期待的不同時,報出了我們在測試文件中,定義的錯誤。并且給出了出錯位置的具體信息。 最后,我們去除最后的冗余的測試信息, 測試通過。 去除冗余信息后,代碼如下: ``` 'use strict'; describe('Service: teacher', function() { // load the service's module beforeEach(module('webAppApp')); // instantiate service var teacher; beforeEach(inject(function(_teacher_) { teacher = _teacher_; })); it('should do something', function() { expect(!!teacher).toBe(true); }); it('應該取出來所有的教師數據', function() { var teachers; teacher.all(function(data) { teachers = data; // 檢測tachers的長度,如果為2,正確,則不報錯。 expect(teachers.length).toBe(2); // 檢測teachers的第一個元素的用戶名是否為zhangsan expect(teachers[0].username).toBe('zhangsan'); }); }); }); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看