Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Jasmine+AngularJS:未加载共享示例_Angularjs - Fatal编程技术网

Jasmine+AngularJS:未加载共享示例

Jasmine+AngularJS:未加载共享示例,angularjs,Angularjs,Jasmine一直抱怨我的一个共享规范中没有测试,即使有以下规范: Spec 'mysite ProductsIndexCtrl behaves like an index controller' has no expectations. 我的等级库文件如下所示: //= require helpers/load-angular-ilook-module //= require products/services/services //= require products/controller

Jasmine一直抱怨我的一个共享规范中没有测试,即使有以下规范:

Spec 'mysite ProductsIndexCtrl behaves like an index controller' has no expectations.
我的等级库文件如下所示:

//= require helpers/load-angular-ilook-module
//= require products/services/services
//= require products/controllers/products_index_controller
//= require helpers/shared_examples_for_products_index_controller

describe('mysite', function() {
  var scope, ProductsIndexCtrl;
  beforeEach(function() {
    module('mysite');
  });

  describe('ProductsIndexCtrl', function() {
    beforeEach(inject(function($controller, $rootScope, $injector) {
      scope = $rootScope.$new();

      $httpBackend = $injector.get('$httpBackend');
      $httpBackend.when('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
      $httpBackend.when('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
      $httpBackend.when('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);

      ProductsIndexCtrl = $controller('ProductsIndexCtrl', {
        '$scope': scope
      });

      $httpBackend.expectGET('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
      $httpBackend.expectGET('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
      $httpBackend.expectGET('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);
      $httpBackend.flush();
    }));


    //HERE!
    it('behaves like an index controller', function() {
      sharedExamplesForIndexControllers(scope);
    });



  });

});
function sharedExamplesForIndexControllers(scope) {

  it('sets color_groups', function() {
    expect(JSON.stringify(scope.color_groups)).toEqual(JSON.stringify([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]));
  });

  it('sets shapes', function() {
    expect(JSON.stringify(scope.shapes)).toEqual(JSON.stringify([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]));
  });

  it('sets materials', function() {
    expect(JSON.stringify(scope.materials)).toEqual(JSON.stringify([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]));
  });

  it('sets color_group_ids', function() {
    expect(scope.color_group_ids).toEqual([]);
  });

  it('sets shape_ids', function() {
    expect(scope.shape_ids).toEqual([]);
  });

  it('sets material_ids', function() {
    expect(scope.material_ids).toEqual([]);
  });

  it('fsdf', function() {
    expect(scope.blah).toEqual("ffffff");
  });

  describe('changeSelectedColor', function() {
    it('sets the selected_color attribute of the product', function() {
      product = {name: "Product Name"};
      color = {name: "Red"};
      scope.changeSelectedColor(product, color);
      expect(product.selected_color).toEqual(color);
    });
  });

  describe('clearAll', function() {
    it('empties the color_group_ids array', function() {
      scope.color_group_ids = [1,2,3];
      scope.clearAll();
      expect(scope.color_group_ids).toEqual([]);
    });

    it('empties the shape_ids array', function() {
      scope.shape_ids = [1,2,3];
      scope.clearAll();
      expect(scope.shape_ids).toEqual([]);
    });

    it('empties the material_ids array', function() {
      scope.material_ids = [1,2,3];
      scope.clearAll();
      expect(scope.material_ids).toEqual([]);
    });

    it('sets selected to false for every object in color_groups', function() {
      expect(scope.color_groups.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_color_groups = true;

      for (var i = 0; i < scope.color_groups.length; i++) {
        var color_group = scope.color_groups[i];
        if (color_group.selected) {
          selected_is_false_for_all_color_groups = false;
          break;
        }
      }

      expect(selected_is_false_for_all_color_groups).toBeTruthy();

    });

    it('sets selected to false for every object in shapes', function() {
      expect(scope.shapes.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_shapes = true;

      for (var i = 0; i < scope.shapes.length; i++) {
        var shape = scope.shapes[i];
        if (shape.selected) {
          selected_is_false_for_all_shapes = false;
          break;
        }
      }

      expect(selected_is_false_for_all_shapes).toBeTruthy();

    });

    it('sets selected to false for every object in materials', function() {
      expect(scope.materials.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_materials = true;

      for (var i = 0; i < scope.materials.length; i++) {
        var material = scope.materials[i];
        if (material.selected) {
          selected_is_false_for_all_materials = false;
          break;
        }
      }

      expect(selected_is_false_for_all_materials).toBeTruthy();

    });
  });


  describe("toggleMaterialFilter", function() {
    it("when material_ids includes material_id: it removes material_id from material_ids", function() {
      scope.material_ids = [1,2,3];
      scope.toggleMaterialFilter(1);
      expect(scope.material_ids).toEqual([2,3]);
    });

    it("when material_ids does not include material_id: it adds material_id to material_ids", function() {
      scope.material_ids = [2,3];
      scope.toggleMaterialFilter(1);
      expect(scope.material_ids).toEqual([2,3,1]);
    });
  });

  describe("toggleShapeFilter", function() {
    it("when shape_ids includes shape_id: it removes shape_id from shape_ids", function() {
      scope.shape_ids = [1,2,3];
      scope.toggleShapeFilter(1);
      expect(scope.shape_ids).toEqual([2,3]);
    });

    it("when shape_ids does not include shape_id: it adds shape_id to shape_ids", function() {
      scope.shape_ids = [2,3];
      scope.toggleShapeFilter(1);
      expect(scope.shape_ids).toEqual([2,3,1]);
    });
  });

  describe("toggleColorFilter", function() {
    it("when color_group_ids includes color_group_id: it removes color_group_id from color_group_ids", function() {
      scope.color_group_ids = [1,2,3];
      scope.toggleColorFilter(1);
      expect(scope.color_group_ids).toEqual([2,3]);
    });

    it("when color_group_ids does not include color_group_id: it adds color_group_id to color_group_ids", function() {
      scope.color_group_ids = [2,3];
      scope.toggleColorFilter(1);
      expect(scope.color_group_ids).toEqual([2,3,1]);
    });
  });
}
这很奇怪,因为我的共享\u示例\u for \u products\u index\u controller.js文件如下所示:

//= require helpers/load-angular-ilook-module
//= require products/services/services
//= require products/controllers/products_index_controller
//= require helpers/shared_examples_for_products_index_controller

describe('mysite', function() {
  var scope, ProductsIndexCtrl;
  beforeEach(function() {
    module('mysite');
  });

  describe('ProductsIndexCtrl', function() {
    beforeEach(inject(function($controller, $rootScope, $injector) {
      scope = $rootScope.$new();

      $httpBackend = $injector.get('$httpBackend');
      $httpBackend.when('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
      $httpBackend.when('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
      $httpBackend.when('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);

      ProductsIndexCtrl = $controller('ProductsIndexCtrl', {
        '$scope': scope
      });

      $httpBackend.expectGET('/api/color_groups').respond([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]);
      $httpBackend.expectGET('/api/shapes').respond([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]);
      $httpBackend.expectGET('/api/materials').respond([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]);
      $httpBackend.flush();
    }));


    //HERE!
    it('behaves like an index controller', function() {
      sharedExamplesForIndexControllers(scope);
    });



  });

});
function sharedExamplesForIndexControllers(scope) {

  it('sets color_groups', function() {
    expect(JSON.stringify(scope.color_groups)).toEqual(JSON.stringify([{"id":1,"name":"Browns","created_at":"2014-08-06T21:38:41.000Z","updated_at":"2014-08-06T21:38:41.000Z"},{"id":2,"name":"Blues","created_at":"2014-08-06T21:38:53.000Z","updated_at":"2014-08-06T21:38:53.000Z"},{"id":3,"name":"Greens","created_at":"2014-08-06T21:39:03.000Z","updated_at":"2014-08-06T21:39:03.000Z"}]));
  });

  it('sets shapes', function() {
    expect(JSON.stringify(scope.shapes)).toEqual(JSON.stringify([{"id":1,"name":"Round","created_at":"2014-05-23T17:10:32.000Z","updated_at":"2014-05-23T17:10:32.000Z"},{"id":2,"name":"Cat Eye","created_at":"2014-05-27T18:53:36.000Z","updated_at":"2014-05-27T18:53:36.000Z"},{"id":3,"name":"sharps","created_at":"2014-05-28T21:41:44.000Z","updated_at":"2014-07-02T14:13:59.000Z"},{"id":4,"name":"square","created_at":"2014-06-02T15:29:58.000Z","updated_at":"2014-06-02T15:29:58.000Z"},{"id":5,"name":"Round","created_at":"2014-06-02T15:35:06.000Z","updated_at":"2014-06-02T15:35:06.000Z"},{"id":6,"name":"fffff","created_at":"2014-07-02T14:14:23.000Z","updated_at":"2014-07-02T14:14:23.000Z"},{"id":7,"name":"Rectangular","created_at":"2014-07-29T15:17:01.000Z","updated_at":"2014-07-29T15:17:01.000Z"},{"id":8,"name":"Diamond","created_at":"2014-07-29T15:17:08.000Z","updated_at":"2014-07-29T15:17:08.000Z"}]));
  });

  it('sets materials', function() {
    expect(JSON.stringify(scope.materials)).toEqual(JSON.stringify([{"id":1,"name":"Steel","created_at":"2014-05-23T17:04:42.000Z","updated_at":"2014-05-23T17:04:42.000Z"},{"id":2,"name":"Cotton","created_at":"2014-05-23T17:04:47.000Z","updated_at":"2014-05-23T17:04:47.000Z"},{"id":3,"name":"woods","created_at":"2014-05-28T20:26:16.000Z","updated_at":"2014-07-02T14:35:09.000Z"},{"id":4,"name":"plastic","created_at":"2014-06-02T15:30:05.000Z","updated_at":"2014-06-02T15:30:05.000Z"},{"id":5,"name":"glass","created_at":"2014-06-02T15:34:58.000Z","updated_at":"2014-06-02T15:34:58.000Z"},{"id":7,"name":"wood","created_at":"2014-06-02T16:51:47.000Z","updated_at":"2014-06-02T16:51:47.000Z"},{"id":8,"name":"rubber","created_at":"2014-07-02T14:35:51.000Z","updated_at":"2014-07-02T14:35:51.000Z"},{"id":9,"name":"carbon","created_at":"2014-07-29T14:52:44.000Z","updated_at":"2014-07-29T14:52:44.000Z"},{"id":10,"name":"paper","created_at":"2014-07-29T14:53:31.000Z","updated_at":"2014-07-29T14:53:31.000Z"},{"id":11,"name":"sandpaper","created_at":"2014-07-29T14:56:39.000Z","updated_at":"2014-07-29T14:56:39.000Z"},{"id":12,"name":"xfsdfsf","created_at":"2014-07-29T14:56:44.000Z","updated_at":"2014-07-29T14:56:44.000Z"}]));
  });

  it('sets color_group_ids', function() {
    expect(scope.color_group_ids).toEqual([]);
  });

  it('sets shape_ids', function() {
    expect(scope.shape_ids).toEqual([]);
  });

  it('sets material_ids', function() {
    expect(scope.material_ids).toEqual([]);
  });

  it('fsdf', function() {
    expect(scope.blah).toEqual("ffffff");
  });

  describe('changeSelectedColor', function() {
    it('sets the selected_color attribute of the product', function() {
      product = {name: "Product Name"};
      color = {name: "Red"};
      scope.changeSelectedColor(product, color);
      expect(product.selected_color).toEqual(color);
    });
  });

  describe('clearAll', function() {
    it('empties the color_group_ids array', function() {
      scope.color_group_ids = [1,2,3];
      scope.clearAll();
      expect(scope.color_group_ids).toEqual([]);
    });

    it('empties the shape_ids array', function() {
      scope.shape_ids = [1,2,3];
      scope.clearAll();
      expect(scope.shape_ids).toEqual([]);
    });

    it('empties the material_ids array', function() {
      scope.material_ids = [1,2,3];
      scope.clearAll();
      expect(scope.material_ids).toEqual([]);
    });

    it('sets selected to false for every object in color_groups', function() {
      expect(scope.color_groups.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_color_groups = true;

      for (var i = 0; i < scope.color_groups.length; i++) {
        var color_group = scope.color_groups[i];
        if (color_group.selected) {
          selected_is_false_for_all_color_groups = false;
          break;
        }
      }

      expect(selected_is_false_for_all_color_groups).toBeTruthy();

    });

    it('sets selected to false for every object in shapes', function() {
      expect(scope.shapes.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_shapes = true;

      for (var i = 0; i < scope.shapes.length; i++) {
        var shape = scope.shapes[i];
        if (shape.selected) {
          selected_is_false_for_all_shapes = false;
          break;
        }
      }

      expect(selected_is_false_for_all_shapes).toBeTruthy();

    });

    it('sets selected to false for every object in materials', function() {
      expect(scope.materials.length).toBeGreaterThan(0);
      scope.clearAll();

      selected_is_false_for_all_materials = true;

      for (var i = 0; i < scope.materials.length; i++) {
        var material = scope.materials[i];
        if (material.selected) {
          selected_is_false_for_all_materials = false;
          break;
        }
      }

      expect(selected_is_false_for_all_materials).toBeTruthy();

    });
  });


  describe("toggleMaterialFilter", function() {
    it("when material_ids includes material_id: it removes material_id from material_ids", function() {
      scope.material_ids = [1,2,3];
      scope.toggleMaterialFilter(1);
      expect(scope.material_ids).toEqual([2,3]);
    });

    it("when material_ids does not include material_id: it adds material_id to material_ids", function() {
      scope.material_ids = [2,3];
      scope.toggleMaterialFilter(1);
      expect(scope.material_ids).toEqual([2,3,1]);
    });
  });

  describe("toggleShapeFilter", function() {
    it("when shape_ids includes shape_id: it removes shape_id from shape_ids", function() {
      scope.shape_ids = [1,2,3];
      scope.toggleShapeFilter(1);
      expect(scope.shape_ids).toEqual([2,3]);
    });

    it("when shape_ids does not include shape_id: it adds shape_id to shape_ids", function() {
      scope.shape_ids = [2,3];
      scope.toggleShapeFilter(1);
      expect(scope.shape_ids).toEqual([2,3,1]);
    });
  });

  describe("toggleColorFilter", function() {
    it("when color_group_ids includes color_group_id: it removes color_group_id from color_group_ids", function() {
      scope.color_group_ids = [1,2,3];
      scope.toggleColorFilter(1);
      expect(scope.color_group_ids).toEqual([2,3]);
    });

    it("when color_group_ids does not include color_group_id: it adds color_group_id to color_group_ids", function() {
      scope.color_group_ids = [2,3];
      scope.toggleColorFilter(1);
      expect(scope.color_group_ids).toEqual([2,3,1]);
    });
  });
}

我做错了什么???如何为我的控制器制作共享示例?

问题是您正在将一个it包装到另一个it中。正如javaCity正确地认为的那样,您不能这样做&需要将对sharedexamplesforindexcontroller的调用封装在描述中

但是,这会导致您描述的第二个问题,即范围未定义。这是因为在执行外部描述时,实际上在beforeach之前调用了对SharedSamplesForIndexController的调用。这样的范围在该点未定义。您需要做的是将范围的获取推迟到每次触发之前,理想情况下,直到您在各种its中真正需要它

实现这一点的一种方法(尽管可能不是最干净的方法)是将一个关闭范围的闭包传递到共享示例中,这样当您在其中执行它时,它就具有您之前分配的值。可能有一种更干净的方法,但这里有一个简单的例子:

  describe('ProductsIndexCtrl', function() {
beforeEach(inject(function($controller, $rootScope, $injector) {
  scope = $rootScope.$new();
  …
  ProductsIndexCtrl = $controller('ProductsIndexCtrl', {
    '$scope': scope
  });

  …
}));


//HERE!
it('behaves like an index controller', function() {
  sharedExamplesForIndexControllers(function () { return scope; });
});
});
})
共享示例:

function sharedExamplesForIndexControllers(scopeGetter) {
  it('sets color_groups', function() {  expect(JSON.stringify(scopeGetter().color_groups)).toEqual(<expected_value>);

你能用它来描述它吗?它的行为就像一个索引控制器,函数…?我试过了,它告诉我:TypeError:“undefined”不是一个计算“scope.color\u group\u id=[2,3]”的对象,我想这是因为如果我不使用它,那么scope就没有定义这是正确的。我的错。