简体   繁体   中英

How do I run a Frisby.js test inside a function

I can't figure out why this frisby tests won't run!

Basically I'm trying to import JSON from a file and check it against a return from a request. The compiler doesn't seem to find any tests when I run this file.

If anyone could possibly suggest a better way to do this? I'm thinking about trying a different way to handle the file reading. I know about readFileSync() but I do not want to use that if I don't have to! Any help would be appreciated.

function readContent(callback,url,file) {
    fs.readFile(file, 'UTF8', function (err, content) {
        if (err) return callback(err)
        data = JSON.parse(content)
        callback(null, data)
    })
}

readContent(function (err, content) {
   frisby.create('Testing API')
    .get(url)
        .expectStatus(200)
        .expectBodyContains(content)
    .toss() 
},
url,file)

Here's one I prepared earlier:

// Read a JSON file from disk, and compare its contents with what comes back from the API.
fs.readFile(path.resolve(__dirname, 'GET_ReferenceTypes.json'), 'utf-8', function(error, data){
  if (error) throw error
  frisby.create('GET ReferenceTypes, inside readFile callback')
    .get(URL + 'ReferenceTypes?requestPersonId=2967&id=99')
    .expectStatus(200)
    // JSON.parse() is required to convert the string into a proper JSON object for comparison.
    // The .replace() strips the BOM character from the beginning of the unicode file.
    .expectJSON(JSON.parse(data.replace(/^\uFEFF/, '')))
  .toss();
});

Double check the encoding on your JSON file, because this whole thing comes apart without the .replace() call.

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