What's a good way to reuse test code using Jasmine?

前端 未结 8 622
無奈伤痛
無奈伤痛 2020-12-05 07:01

I\'m using the Jasmine BDD Javascript library and really enjoying it. I have test code that I\'d like to reuse (for example, testing multiple implementations of a base clas

8条回答
  •  星月不相逢
    2020-12-05 07:45

    This is similar to starmer's answer, but after working through it I found some differences to point out. The downside is that if the spec fails you just see 'should adhere to common saving specifications' in the Jasmine report. The stack trace is the only way to find where it failed.

    // common specs to execute
    self.executeCommonSpecifications = function (vm) {
      // I found having the describe( wrapper here doesn't work
      self.shouldCallTheDisplayModelsSaveMethod(vm);
    }
    self.shouldCallTheDisplaysSaveMethod = function (vm) {
      expect(vm.save.calls.count()).toBe(1);
    };
    
    // spec add an it so that the beforeEach is called before calling this
    beforeEach(function(){
      // this gets called if wrapped in the it
      vm.saveChanges();
    }
    it('should adhere to common saving specifications', function () {
      executeSavingDisplaysCommonSpecifications(vm);
    });
    

提交回复
热议问题