笑话测试功能绑定

时间:2019-02-28 15:55:34

标签: javascript jestjs bind

如何绑定某物开玩笑地执行测试功能?

我想针对不同的配置运行两次相同的测试集,如下所示:

function wrap(title, fn) {
    [1,2].forEach(x => {
        this.hello = x
        fn.bind(this)
        fn() // works
        test(title, fn) // does not work
    })
}

wrap('test hello world', function() {
    console.log('this.hello', this.hello)
});

但是当开玩笑时thisundefined

2 个答案:

答案 0 :(得分:0)

结果开玩笑提供了一个内置的帮助程序来完成此操作:

describe.each([1,2])('test', x => {
    test(`test ${x}`, function() {
        console.log('x', x)
    });
})

答案 1 :(得分:0)

HTML CODE : 

<!-- createNewAbout is my module name and submitAboutContent is the Controller You you can call it the backend function . 

您也可以在“表单操作”中使用URL。 ->                         输入标题                    输入说明                    添加图片                    提交          

<!-- this is the function Which i am Calling . I am taking value of  name attribute of button to disable button for multiple fuction call and also taking an array of values of name attribute of  manditory fields. Here we cant use values of ID attribute so we have to use name . -->
<script> 
function validationOnlySubmitForm(button_name="", arr="") {
var fields = arr, flag  = 0;
document.getElementsByName(button_name)[0].setAttribute("disabled" , true);
for (var i = 0 ; i <= fields.length - 1 ; i++) {
if (document.getElementsByName(fields[i])[0].value.length == 0) {
flag = 1;
document.getElementsByName(fields[i])[0].focus();
alert(fields[i] + " is manditory.");
document.getElementsByName(button_name)[0].removeAttribute("disabled");
return false;
}
}

if (flag == 0)
return true;
}

</script>