7

I started using Jasmine to test, angularjs, but soon I started wondering which of the approach is good, and why ? I chose the single expect per spec. I liked it because a non-techie can understand it better, well, that is the point of BDD ,is not it ? For example

describe('Testing multple expect per spec My Service', function() {
 beforeEach(/* some stuff*/);
   it('test part ',function(){
       expect('part 1').toBe('part one'); // 
       expect('part 2').toBe('part two'); // some crazy stuff, just for example
     }
}

describe('Testing single expect per spec My Service', function() {
     beforeEach(/* some stuff*/);
       it('test part  one ',function(){
           expect('part 1').toBe('part one'); // just for example
         }
       it('test part  two ',function(){
            expect('part 2').toBe('part two'); // just for example
         }
}

But soon got into trouble, I realized that the beforeEach is actually reseting/ creating a new object of my service ( not shown in the above example) thus the encapsulated behavior is not reflecting correctly.

For example, I am testing the firstMethod() part of a injected service which changes a data structure of the service, 'test part one', then in the second spec i am testing the secondMethod() which tests the change data structure in the 'test part two'. But due to the fact that beforeEach re-injected my service, the 'test part two' fails. How can I get around such situation ?

Please help Jasmine newbie here.

3
  • 2
    For your understanding - the reason that each spec is isolated (new object in the beforeEach) is to allow multi threading. Many specs can run at the same time (and the order doesn't matter). See answer(s) for why this is a good idea from test design standpoint.
    – ktharsis
    Commented Aug 14, 2015 at 12:58
  • Interesting, if jasmine is running in browser based test cases, how the multi threading come in ? Commented Aug 17, 2015 at 4:39
  • The test runner itself would have to do that (Karma, Chutzpah, etc). Not sure if/which actually do - probably very few. The primary reason for the separation is better test design (as explained in answers) - concurrency is just a side effect that is possible because of that framework.
    – ktharsis
    Commented Aug 17, 2015 at 20:48

1 Answer 1

7

Your unit tests should be independent of each other. Otherwise, if Test 2 fails, you don't know if there is a problem in Method 2, or if there is a problem with the setup that was done in Method 1.

To handle this situation, you can do some additional setup at the start of Test 2 where you ensure the service is in whatever state necessary in order to test the logic in Method 2. If multiple tests will require that setup, you can put it in a nested describe and beforeEach.

As @ktharsis notes, multiple expects per spec are fine as long as they are verifying the same "behavior". Each test should correspond to one behavior, not necessarily one assert.

1
  • 4
    It should also be noted, there is nothing inherently wrong with multiple expects per spec as long as they are verifying the same "behavior". For example "method should return true for strings longer than 4" could be setup as a single spec with expects for 3, 4, and 5 length instead of 3 different specs.
    – ktharsis
    Commented Aug 14, 2015 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.