【问题标题】:Why am I getting a 404 accessing this file via karma test为什么我通过业力测试获得 404 访问此文件
【发布时间】:2018-08-21 11:35:00
【问题描述】:

MCVE 位于this github repo

我正在开发一个加载时读取清单文件的 javascript 应用程序。我希望能够测试该功能是否有效。我有这个测试功能:

it('We can get a manifest file', function(doneFn) {
        manifest=""
        console.log(window.location.pathname);
        console.log("1")
        load_manifest("/Users/joepublic/MCVE/manifest.json");

        console.log("2")
        console.log(manifest.length)
        setTimeout(expect(manifest.length).not.toBeLessThan(1),4000);
        console.log(manifest);
        doneFn();
        });         
    });

我正在测试的函数如下所示:

function load_manifest(filename){
    //go and get a manifest. We're starting with the one in the root of the template for now.
      var req = new XMLHttpRequest();
          console.log("a"); 
      req.open("GET", filename);
      req.send(null);
    req.onload = function() {

        if (req.readyState == 4 && req.status == 200) {
           console.log("hello"); 
           console.log(req.responseText); 
           manifest= JSON.parse(req.responseText);
        } else {
            console.log("Problem reading file");
        }
    };
          console.log("b"); 

} 

业力的输出是:

MacBook-Air:MCVE joepublic$ karma start my.conf.js
21 08 2018 12:39:51.453:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2018 12:39:51.466:INFO [karma]: Karma v3.0.0 server started at http://0.0.0.0:9876/
21 08 2018 12:39:51.467:INFO [launcher]: Launching browser Chrome with unlimited concurrency
21 08 2018 12:39:51.482:INFO [launcher]: Starting browser Chrome
21 08 2018 12:39:57.270:INFO [Chrome 68.0.3440 (Mac OS X 10.13.4)]: Connected on socket yZegecjIaWSc9KrEAAAA with id 47010677
21 08 2018 12:39:57.507:WARN [web-server]: 404: /Users/joepublic/MCVE/manifest.json
LOG: '/context.html'
LOG: '1'
LOG: 'a'
LOG: 'b'
LOG: '2'
LOG: 0
LOG: ''
LOG: 'Problem reading file'
Chrome 68.0.3440 (Mac OS X 10.13.4) Displaying OBF on the index page Test Name property We can get a manifest file FAILED
    Expected 0 not to be less than 1.
        at <Jasmine>
        at UserContext.<anonymous> (read_data_test.js:30:43)
        at <Jasmine>
Chrome 68.0.3440 (Mac OS X 10.13.4): Executed 1 of 1 (1 FAILED) ERROR (0.059 secs / 0.012 secs)

我收到 404 - 但“ls /Users/joepublic/MCVE/manifest.json”发现文件正常...这是怎么回事?

【问题讨论】:

  • 你的 else 将在 readystate
  • @JaromandaX 很有用。谢谢,关于让函数触发的任何想法?
  • 它触发了:21 08 2018 12:39:57.507:WARN [web-server]: 404: /Users/joepublic/MCVE/manifest.json; LOG: 'Problem reading file'。注意 404 错误。
  • @D.Pardal 你是对的 - 我会编辑。

标签: javascript testing jasmine karma-runner


【解决方案1】:
404: /Users/joepublic/MCVE/manifest.json

URL http://localhost:9876/Users/joepublic/MCVE/manifest.json 是 404 错误。

但是“ls /Users/joepublic/MCVE/manifest.json”发现文件很好

这是您本地文件系统上的路径。它不是提供的 HTTP URL,而是您的测试正在运行的 Web 服务器。

【讨论】:

  • 啊-我们是说我需要运行本地主机吗?或者我应该在代码中有一些东西?
  • “啊——我们是说我需要运行一个本地主机吗?” — 不,您正在从 localhost 运行它。
  • "或者我应该在代码中有一些东西?" — 你应该有一个相对于项目根目录(或者你的测试运行放置其 HTTP 服务器的根目录)的路径,而不是本地文件路径。
猜你喜欢
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多