[英]Jasmine: TypeError: Cannot read property 'toLowerCase' of undefined
I am having a function which checks if name exists in an array (names_array). 我有一个检查名称是否存在于数组(names_array)中的函数。 If exists return true else if not then push the name to names_array and create table rows for each name and append to table.
如果存在,则返回true,否则返回true,然后将名称推送到names_array并为每个名称创建表行并追加到表中。
Using Jasmine I have to test if the function is returning true or false. 使用Jasmine,我必须测试该函数是否返回true或false。 For this I am passing values for variables names_array and name.
为此,我要传递变量names_array和name的值。 But still getting undefined variable error for the code $('#add_name').val();
但是对于代码$('#add_name')。val();仍然出现未定义的变量错误。
My javascript code to test is... 我要测试的JavaScript代码是...
function check_names(names_array) {
var name = $('#add_name').val().toLowerCase();
//check if name is there in names_array if yes return true, if not then return false and push the item to the names_array
}
Jasmine code which I am using.. 我正在使用的茉莉花代码
describe("On Name Create",function (){
var names_array = ["add","bad","sad","dad"];
it("Check for names",function (){
var name = "add";
expect(check_names(names_array)).toBeTruthy();
});
});
Am I missing something as I am new to Jasmine? 当我刚接触茉莉花时,我会错过什么吗?
You should first check that $('#add_name).val() is not null. 您应该首先检查$('#add_name).val()是否为null。
Also maybe you should try $('#add_name).eq(0).val() 另外也许你应该尝试$('#add_name).eq(0).val()
you are getting this error because $('#add_name').val()
not providing any value to toLowerCase()
function. 因为
$('#add_name').val()
没有为toLowerCase()
函数提供任何值,您会收到此错误。
Make sure #add_name
, this id in your HTML code have some value with it. 确保
#add_name
,它在您的HTML代码中具有此ID。 You can print it before it's use in your code. 您可以先打印它,然后再在代码中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.