简体   繁体   English

如何在函数内运行Frisby.js测试

[英]How do I run a Frisby.js test inside a function

I can't figure out why this frisby tests won't run! 我无法弄清楚为什么这个frisby测试不会运行!

Basically I'm trying to import JSON from a file and check it against a return from a request. 基本上我正在尝试从文件中导入JSON并根据请求的返回进行检查。 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! 我知道readFileSync()但我不想使用它,如果我不需要! 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. 仔细检查JSON文件上的编码,因为没有.replace()调用,这一切都会分开。

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

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