简体   繁体   中英

Why am I getting a 404 accessing this file via karma test

MCVE is at this github repo .

I'm working on a javascript application that reads a manifest file when loaded. I'd like to be able to test that the function works. I've got this test function:

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();
        });         
    });

and the function I'm testing looks like this:

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"); 

} 

The output from karma is:

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)

I'm getting a 404 - but "ls /Users/joepublic/MCVE/manifest.json" finds the file fine...what's going on?

404: /Users/joepublic/MCVE/manifest.json

The URL http://localhost:9876/Users/joepublic/MCVE/manifest.json is a 404 error.

but "ls /Users/joepublic/MCVE/manifest.json" finds the file fine

That is a path on your local file system. It is not an HTTP URL served up but the web server that your test running is running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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