title: Angular + Jasmine Testing: Mock testing with arrays
body: <p>I'm writing some Jasmine tests for my Campaigns service and I'd like to be able to create <code>mockRepo</code> array in my CampaignServices spec. The GET test is working, however, with CREATE, I'd like to add a new campaign to the array and unsure how I'd do that in my Jasmine test.</p>
  
  <p>I've read over the Jasmine documentation, but can't see an elegant way where I could push a new array to the end by incrementing the ID count of a new campaign and then expecting that to match within the test. </p>
  
  <p>Please find below the Campaigns model and service:
  <strong>Model: Campaigns.js</strong></p>
  
  <pre><code>var portal;
  (function (portal) {
      var campaigns;
      (function (campaigns) {
          var Campaign = (function () {
              function Campaign(id, name, description, status, start, end, createdAt, updatedAt) {
                  this.id = id;
                  this.name = name;
                  this.description = description;
                  this.status = status;
                  this.start = start;
                  this.end = end;
                  this.createdAt = createdAt;
                  this.updatedAt = updatedAt;
              }
              return Campaign;
          })();
          campaigns.Campaign = Campaign;
      })(campaigns = portal.campaigns || (portal.campaigns = {}));
  })(portal || (portal = {}));
  </code></pre>
  
  <p><strong>Service: CampaignsService.js</strong></p>
  
  <pre><code>var portal;
  (function (portal) {
      var campaigns;
      (function (campaigns) {
          'use strict';
          var CampaignsService = (function () {
              function CampaignsService(api) {
                  this.mockRepo = [
                      new Campaign(1, 'Campaign 1', 'My new campaign', 'live', new Date(2015, 6, 2), new Date(2016, 3, 5), new Date(2015, 6, 2), new Date(2016, 3, 5)),
                      new Campaign(2, 'Campaign 2', 'My old campaign', 'complete', new Date(2014, 3, 3), new Date(2015, 3, 5), new Date(2015, 6, 2), new Date(2016, 3, 5))
                  ];
                  this.__resource = api.all('campaigns');
              }
              CampaignsService.prototype.getList = function () {
                  return this.mockRepo;
                  //return this.__resource.getList();
              };
              CampaignsService.prototype.get = function (id) {
                  return this.mockRepo.filter(function (item) {
                      return item.id === id;
                  })[0];               
              };
              CampaignsService.prototype.update = function (model) {
                  var theCampaign = this.get(model.id);
                  theCampaign.name = model.name;
                  theCampaign.updatedAt = new Date();
  
              };
              CampaignsService.prototype.create = function (model) {
                  model.id = this.mockRepo.length + 1;
                  this.mockRepo.push(model);
                  return model.id;
              };
              CampaignsService.prototype.delete = function (id) {
                  this.mockRepo = this.mockRepo.filter(function (item) {
                      return item.id !== id;
                  });
              };
              CampaignsService.$inject = ["Restangular"];
              return CampaignsService;
          })();
          campaigns.CampaignsService = CampaignsService;
          angular.module('olCampaigns').service('CampaignsService', CampaignsService);
      })(campaigns = portal.campaigns || (portal.campaigns = {}));
  })(portal || (portal = {}));
  </code></pre>
  
  <p>Below is my spec with the GET passing, however, I haven't managed to CREATE a new campaign. Any suggestions on how best to implement that would be greatly appreciated. </p>
  
  <p><strong>Spec: CampaignServices.js</strong></p>
  
  <pre><code>describe('Campaigns Service', function() {
      var campaigns = [];
  
      beforeEach(function() {
          campaigns = [
              {
                "id": "1",
                "name": "Campaign 1",
                "description": "My new campaign",
                "status": "live",
                "start": new Date(2015, 6, 2),
                "end": new Date(2016, 3, 5),
                "createdAt": new Date(2015, 6, 2),
                "updatedAt": new Date(2016, 3, 5)
              },
              {
                "id": "2",
                "name": "Campaign 2",
                "description": "My old campaign",
                "status": "complete",
                "start": new Date(2014, 3, 3),
                "end": new Date(2015, 3, 5),
                "createdAt": new Date(2015, 6, 2),
                "updatedAt": new Date(2016, 3, 5)
              }
          ]
      });
  
      it('GET ALL campaigns', function() {
          expect(campaigns.length).toEqual(2);
          expect(campaigns[0]).toEqual({"id": "1","name": "Campaign 1","description": "My new campaign","status": "live","start": new Date(2015, 6, 2),"end": new Date(2016, 3, 5),"createdAt": new Date(2015, 6, 2),
          "updatedAt": new Date(2016, 3, 5)});
      });
  
      it('CREATE a campaign', function() {
          // Want to create a new campaign and add it to the campaigns array above
          newCampaign.id = this.campaignMocks.length + 1;
          this.campaigns.push(newCampaign);
          console.log(newCampaign.id)
      });
  });
  </code></pre>