繁体   English   中英

茉莉花测试url的javascript函数

[英]Jasmine test the javascript function for url

这是我的代码:

var getCode = function(code) {
if (code = (new RegExp('[?&]' + encodeURIComponent(code) + '=([^&]*)'))
        .exec(location.search))
    return decodeURIComponent(code[1]);
};

$(document).ready(function() {
var code = getCode("code");
//some other function

这是我的测试代码:

 it("check the code value of url", function() {
    window.location.search = "https://test.com?&code=testParam";
    var code = getCode("code");
    expect(code).toEqual("testParam");
});

每当我尝试将其作为茉莉花测试运行时,它都会显示runtimeException或说未定义变量getCode。 无论如何,我可以设置位置网址并进行测试吗?

非常感谢您的帮助

我认为您应该做的差不多。 不要从浏览器获取URL。 而是将url作为参数传递给函数。 这样,您可以对网址进行硬编码并发送给测试。

var getCode = function(code, url) {
    if (code = (new RegExp('[?&]' + encodeURIComponent(code) + '=([^&]*)'))
    .exec(url))
  return decodeURIComponent(code[1]);
};

并像下面这样运行测试。

it("check the code value of url", function() {
var url = "https://test.com?&code=testParam";
var code = getCode("code", url);
expect(code).toEqual("testParam");
});

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM