使用localStorage运行测试

使用localStorage运行测试

本文介绍了使用localStorage运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循Codelab和yeoman的教程。当实施正确时,您正在使用本地存储来存储TodoList。我在设置我的测试时遇到问题,以测试它是否有效。这是我到目前为止:

 'use strict'; 
$ b $ describe('Controller:MainCtrl',function(){

//载入控制器的模块
beforeEach(module('yeoTodoApp'),module(' LocalStorageModule'));

var MainCtrl,
scope;

//初始化控制器和模拟范围
beforeEach(inject(function控制器,$ rootScope,$ httpBackend){$ b $ 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);
$ b);
it('应该添加项目到列表然后删除',function(){
var beforeLength = scope.todos.length;
scope.todo ='测试1 ';
scope.addTodo();
scope.removeTodo(0);
var afterLength = scope.todos.length;
expect(afterLength-beforeLength).toBe(0);
});

});

我得到的错误是

我如何编写单元测试来存储本地存储?

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

 控制器:MainCtrl应该添加项目到列表然后删除失败

this错误是一个简单的测试错误,这意味着你的代码在某处并不像预期的那样工作(你的第二次测试失败)。

我自己会检查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'为'0'

去那里!


i have followed this tutorial from Codelab and yeoman. When implemented right you are using local storage to store the TodoList. I have problems with setting up with my tests, to test if this works. This is what i've got so far:

'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);
  });

});

The error i get is

How would i write my unit tests to sit the local storage?

解决方案

your setup is correct now (after you removed $httpBackend from the arguments list)

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

this error is a simple test error, which means that your code somewhere doesnt work as expected (your second test fails)

i for myself would check todos length, and not the result of a mathematical operation.

i would write your tests the test like this:

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);
});

you use jasmine as a test-tool. jasmine logs on errors exactly which expectation fails, so you should get something like

expect '1' to be '0'

go from there!

这篇关于使用localStorage运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:38