2016-10-26 70 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); 
}); 
相关问题