2017-07-27 109 views
0

我需要檢查特定方法的多個變量值。在單元測試中測試多個變量值?

目前,我有以下幾點:

describe('Example Test', function(){ 
    it('tests multiple input values', function(){ 
     expect(function(valOne)).toBe('testOne'); 
     expect(function(valTwo)).toBe('testTwo'); 
     expect(function(valThree)).toBe('testThree'); 

    }); 
}); 

其中,在未能返回以下消息:

Failures: 
1)Example Test tests multiple input values 
1.1) Expected 'testVal' to be 'testOne'. 

我知道我可以將它們一起在同一個想到聲明如下:

describe('Example Test', function(){ 
    it('tests multiple input values', function(){ 
     expect(
      function(valOne) === 'testOne' && 
      function(valTwo) === 'testTwo' && 
      function(valThree) ==='testThree' 
     ).toBeTruthy(); 
    }); 
}); 

很顯然,在這種情況下,失敗消息只是表明錯誤整個狀況的即將倒閉沒有指明哪個特定條件(S)失敗:

Failures: 
1)Example Test tests multiple input values 
1.1) Expected false to be truthy. 

正在使用多個except()語句來實現這一點的正確方法?

在單元測試中測試多個值的最佳做法是什麼時候/如果失敗我確切知道哪個條件/值失敗?

+0

第一種方式有問題嗎? – jonrsharpe

+0

@jonrsharpe不,沒有問題,它的工作原理。我更想知道這是可接受的做法還是有更好的方法來做到這一點。 –

+1

我會說第一種方式更好,因爲它更容易看到**在測試中錯誤發生在哪裏** – 0mpurdy

回答

1

我會說第一種方式更好,因爲它更容易看到其中在測試中發現錯誤。