2016-10-26 95 views
1

是否有任何方法在一次測試中有多個參數,而不是再次複製和粘貼該函數?在NUnit的JS單元測試使用不同參數運行多次

C的例子#:

[TestCase("0", 1)] 
[TestCase("1", 1)] 
[TestCase("2", 1)] 
public void UnitTestName(string input, int expected) 
{ 
    //Arrange 

    //Act 

    //Assert 
} 

我想在JS:

describe("<Foo />",() => { 

    [TestCase("false")] 
    [TestCase("true")] 
    it("option: enableRemoveControls renders remove controls", (enableRemoveControls) => { 
     mockFoo.enableRemoveControls = enableRemoveControls; 

     //Assert that the option has rendered or not rendered the html 
    }); 
}); 
+0

只是做一個新的功能,並稱它,我猜。一般來說,因爲你正在向'it'(和'describe'等)傳遞一個回調函數,所以你可以創建一個返回回調函數的函數makeTest(input,expected){return function(){assert(input == =「expected」)})''然後在測試中調用它it(「pass」,makeTest(1,1))'並改變它的參數it(「failed」,makeTest(「apple」,「orange」)) ' – vlaz

回答

2

你可以把it -call在函數內部,並使用不同的參數調用它:

describe("<Foo />",() => { 

    function run(enableRemoveControls){ 
     it("option: enableRemoveControls renders remove controls",() => { 
      mockFoo.enableRemoveControls = enableRemoveControls; 

      //Assert that the option has rendered or not rendered the html 
     }); 
    } 

    run(false); 
    run(true); 
}); 
相關問題