AngularJs变量没有从Jasmine测试中设置

时间:2016-06-24 18:25:45

标签: angularjs jasmine

角色和茉莉花的新手。我的测试中有一种情况,即我的expect语句中的变量没有被声明为正确的值,即使它们在我的控制器中按预期工作。这是我的实际控制器代码,我正在编写jasmine单元测试,基本上是对用户点击执行保存操作:

function performSaveTrackActions() {

  var releaseSlug = vm.selected.album.slug;
  var trackSlug = vm.selected.track.fileName;
  var trackData = vm.formTrack;

  // calls the backend service to save the data
  var trackSaveResponse = trackService.saveTrack(releaseSlug, trackSlug, trackData, vm.currentWorkspace).then(function(saveTrackResponse) {

    if (saveTrackResponse.status == 202) {

      statusMessageService.successMsg("Successfully saved the track " + vm.selected.track.title);
      vm.selected.track = vm.formTrack; // Assigning the original var to modified after successful save
      return releaseService.currentRelease(releaseSlug, vm.currentWorkspace);

    } else {
      statusMessageService.errorMsg("Error: Could not save the track " + vm.selected.track.title);
    }
  }).then(function(response) {
    vm.selected.album.data.tracks = response.data.tracks;
    vm.selectTrack(vm.selected.album, vm.selected.track);
    setTrackLevelDates();
  });
  return trackSaveResponse;
}

这是我的测试,其中行vm.selected.track = vm.formTrack应该正确分配,但不是出于某种原因而且期望失败:

describe('Test for Happy path when saving a track', function() {
      beforeEach(inject(function($q) {
        mockStatusMessageService.successMsg.and.returnValue("Successfully saved the track " + vm.selected.track.title);
        mockTrackService.saveTrack.and.returnValue($q.when(mockTrackSaveResponse));
        vm.formTrack = mockFormTrack;
        vm.selected.track = anotherMockedOriginalData
      }));
      it('interactions when a track is successfully saved', function(done) {

        vm.performSaveTrackActions().then(function() {

          // after successful save, I expect that the two values should be same but they error out saying both are different.
          expect(vm.selected.track.albumArtist).toEqual(mockFormTrack.albumArtist);
        });

        scope.$apply();
        done();
      });
    ...
});

有什么想法吗?

0 个答案:

没有答案