Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gruntjs 使用localStorage运行测试_Gruntjs_Yeoman_Yeoman Generator Angular - Fatal编程技术网

Gruntjs 使用localStorage运行测试

Gruntjs 使用localStorage运行测试,gruntjs,yeoman,yeoman-generator-angular,Gruntjs,Yeoman,Yeoman Generator Angular,我遵循了Codelab和yeoman的教程。正确实施后,您将使用本地存储来存储TodoList。我在设置测试时遇到了问题,无法测试这是否有效。到目前为止,我得到的是: 'use strict'; describe('Controller: MainCtrl', function () { // load the controller's module beforeEach(module('yeoTodoApp'), module('LocalStorageModule'));

我遵循了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);
  });

});
我得到的错误是

第12行第68列“$httpBackend”已定义,但从未使用过。 });

如何编写单元测试以安装本地存储?

您的设置现在是正确的(从参数列表中删除$httpBackend后)

此错误是一个简单的测试错误,这意味着您的代码在某个地方无法按预期工作(第二次测试失败)

我自己会检查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作为测试工具。jasmine记录的错误正好是预期失败的错误,所以您应该得到如下结果

expect '1' to be '0'
从那里开始

您的设置现在是正确的(从参数列表中删除$httpBackend后)

此错误是一个简单的测试错误,这意味着您的代码在某个地方无法按预期工作(第二次测试失败)

我自己会检查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作为测试工具。jasmine记录的错误正好是预期失败的错误,所以您应该得到如下结果

expect '1' to be '0'
从那里开始

您的设置现在是正确的(从参数列表中删除$httpBackend后)

此错误是一个简单的测试错误,这意味着您的代码在某个地方无法按预期工作(第二次测试失败)

我自己会检查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作为测试工具。jasmine记录的错误正好是预期失败的错误,所以您应该得到如下结果

expect '1' to be '0'
从那里开始

您的设置现在是正确的(从参数列表中删除$httpBackend后)

此错误是一个简单的测试错误,这意味着您的代码在某个地方无法按预期工作(第二次测试失败)

我自己会检查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作为测试工具。jasmine记录的错误正好是预期失败的错误,所以您应该得到如下结果

expect '1' to be '0'

从那里开始

我认为目前的想法是在嘲弄您的本地存储:

编写单元测试

对于额外的挑战,在步骤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']); 
  });
});

我认为目前这个想法有点嘲弄你的本地存储:

编写单元测试

对于额外的挑战,在步骤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']); 
  });
});

我认为目前这个想法有点嘲弄你的本地存储:

编写单元测试

对于额外的挑战,在步骤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']); 
  });
});

我认为目前这个想法有点嘲弄你的本地存储:

编写单元测试

对于额外的挑战,在步骤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']); 
  });
});

您的错误是jslint错误,只需从函数参数中删除$httpBackend…谢谢您的帮助。如果我从函数参数中删除$httpBackend,我将得到与没有localstorage时相同的测试
Controller:MainCtrl应将项目添加到列表中,然后删除失败的
您的错误是jslint错误,只需从函数参数中删除$httpBackend…谢谢您的帮助。如果我从函数参数中删除$httpBackend,我将得到与没有localstorage时相同的测试
Controller:MainCtrl应将项目添加到列表中,然后删除失败的
您的错误是jslint错误,只需从函数参数中删除$httpBackend…谢谢您的帮助。如果我从函数参数中删除$httpBackend,我将得到与没有localstorage时相同的测试
Controller:MainCtrl应将项目添加到列表中,然后删除失败的
您的错误是jslint错误,只需从函数参数中删除$httpBackend…谢谢您的帮助。如果我从函数参数中删除$httpBackend,我将得到与没有localstorage时相同的测试
Controller:MainCtrl应将项目添加到列表中,然后删除失败的