lundi 24 avril 2017

Jasmine unit test code duplication

i recently started learning how to write unit test case using jasmine for my angular project.

i was writing test for my service and facing a problem

describe        
    test case 1
        1. check some assertion here
        mock data using provider
        2. check some assertion here after mocking
    test case 2
        3. check some assertion here
        mock data using provider
        4. check some assertion here after mocking

  • test case 1 and test case 2 specify different scenarios and must be done separately .
  • Assertion 1 and 3 should be done before mocking the data
  • Assertion 2 and 4 should be done after mocking the data

Problem is that my code to mock data is duplicate in both test cases, i can't move it in one beforeEach because in that case how i will check assertion 1 and 3 ?

a possible solution may be (but not sure)

describe 
    describe        
        test case 1
            1. check some assertion here
            2. check some assertion here
        test case 2
            3. check some assertion here
            4. check some assertion here    
    describe   
        beforeEach 
            mock data using provider
        test case 1
            1. check some assertion here
            2. check some assertion here
        test case 2
            3. check some assertion here
            4. check some assertion here

but again duplicate code.

Please tell me what should be the best way to handle this

Aucun commentaire:

Enregistrer un commentaire