0

私はCodelab と yeoman のこのチュートリアルに従いました。正しく実装すると、ローカル ストレージを使用して TodoList を保存できます。これが機能するかどうかをテストするために、テストのセットアップに問題があります。これは私がこれまでに持っているものです:

'use strict';

describe('Controller: MainCtrl', function () {

  // load the controller's module
  beforeEach(module('yeoTodoApp'), module('LocalStorageModule'));

  var MainCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
    scope = $rootScope.$new();
    MainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));

  it('should add items to the list', function () {
    var beforeLength = scope.todos.length;
    scope.todo = 'Test 1';
    scope.addTodo();
    var afterLength = scope.todos.length;
    expect(afterLength-beforeLength).toBe(1);
  });
it('should add items to the list then remove', function () {
    var beforeLength = scope.todos.length;
    scope.todo = 'Test 1';
    scope.addTodo();
    scope.removeTodo(0);
    var afterLength = scope.todos.length;
    expect(afterLength-beforeLength).toBe(0);
  });

});

私が得るエラーは

line 12 col 68 '$httpBackend' が定義されていますが、使用されていません。});

ローカル ストレージを配置するための単体テストをどのように作成すればよいですか?

4

2 に答える 2

2

現時点では、このアイデアはローカル ストレージをあざけるようなものだと思います。

単体テストを書く

追加の課題として、ステップ 8 の単体テストに戻り、コードがローカル ストレージを使用するようになったので、テストを更新する方法を検討してください。

ヒント: これは単純な答えではなく、モック サービスについての知識が必要です。AngularJS での単体テストのベスト プラクティス、特に AngularJS でのサービスとモジュールのモッキング セクションを確認してください。

この質問がされてから、状況が変わった可能性があります。とにかく、ここに私の解決策があります:

'use strict';

describe('Controller: MainCtrl', function () {

  // load the controller's module
  beforeEach(module('mytodoyoappApp'));

  var MainCtrl,
    scope,
    localStorage, store;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();

    MainCtrl = $controller('MainCtrl', {
      $scope:scope
      // place here mocked dependencies
    });

    /*mock the localStorageService*/
    store={};

    localStorage = {
      set: function(key, value) {
        store[key] = value;
      },
      get: function(key) {
        return store[key];
      }
    };

  }));

  it('should check the list length', function () {
    expect(MainCtrl.todos.length).toBe(0);
  });

  it('should add items to the list', function () {
    MainCtrl.todoadded = 'Test 1';
    MainCtrl.addTodo();
    expect(MainCtrl.todos.length).toBe(1);
  });

  it('should add then remove an item from the list', function () {
    MainCtrl.todoadded = 'Test 2';
    MainCtrl.addTodo();
    MainCtrl.removeTodo(0);
    expect(MainCtrl.todos.length).toBe(0);
  });

  it('should check that the localstorage is undefined before being set', function() {
    var a=localStorage.get('todos');
    expect(a).toBeUndefined();
  });

  it('should set and get the localstorage', function() {
    localStorage.set('todos', ['Test 3']);
    var a=localStorage.get('todos');
    expect(a).toEqual(['Test 3']);

    localStorage.set('todos', ['Test 4']);
    var b=localStorage.get('todos');
    expect(b).toEqual(['Test 4']); 
  });
});
于 2016-05-19T12:20:53.083 に答える
1

セットアップは正しいです(引数リストから $httpBackend を削除した後)

Controller: MainCtrl should add items to the list then remove FAILED

このエラーは単純なテスト エラーです。つまり、どこかのコードが期待どおりに動作しないことを意味します (2 番目のテストは失敗します)。

私自身は、数学演算の結果ではなく、todos の長さをチェックします。

私はあなたのテストを次のように書きます:

it('should add items to the list then remove', function () {
  scope.todo = 'Test 1';
  expect(scope.todos.length).toBe(0);
  scope.addTodo();
  expect(scope.todos.length).toBe(1);
  scope.removeTodo(0);
  expect(scope.todos.length).toBe(0);
});

テストツールとしてジャスミンを使用します。jasmine は、期待どおりに失敗したエラーを記録するため、次のような結果が得られるはずです。

expect '1' to be '0'

そこから行け!

于 2014-06-23T10:33:58.200 に答える