我在用这个Frisby.js API测试框架进行一次新的测试时遇到了一些困难。
对于上下文,我编写了一些不需要从磁盘读取引用文件的其他测试,并运行了Frisby附带的一些示例,它们都运行得非常快和准确。非常喜欢执行到目前为止的速度。考虑到这一切是如何回来的,我相当肯定我的环境是健全的。
下面是一个有问题的JavaScript文件的简化版本,我正在通过jasmine运行该文件:
var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var URL = 'http://server/api/';
var getJSON = fs.readFileSync(path.resolve(__dirname, 'GET.json'), 'utf-8').replace(/^\uFEFF/, ''); // the .replace removes the BOM from the start of the file
console.log(getJSON); // this dumps the file contents to the screen, no problems here
frisby.create('GET from API')
.get(URL + 'Endpoint?Parameters=Values')
.expectStatus(200) // tests that HTTP Status code equals expected 200, still no worries
.expectJSON(getJSON) // this is where the 'undefined' error is thrown
.toss();从命令行运行测试非常直接:
C:\Testing\Frisby> jasmine-node apitest.js我相信我正在按正确的顺序进行操作,读取同步文件,然后调用Frisby,但是在执行时它会引发以下错误:
Failures:
1) Frisby Test: GET from API
[ GET http://server/api/Endpoint?Parameters=Values ]
Message:
TypeError: Expected valid JavaScript object to be given, got undefined
Stacktrace:
TypeError: Expected valid JavaScript object to be given, got undefined
at _jsonContains (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1182:11)
at jasmine.Matchers.toContainJson (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1141:12)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:686:24)
at null.<anonymous> (C:\Users\jlucktay\AppData\Roaming\npm\node_modules\frisby\lib\frisby.js:1043:43)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Finished in 0.292 seconds
1 test, 2 assertions, 1 failure, 0 skipped我已经通过npm全局安装了jasmine和frisby包,并且使用了npm link frisby,它创建了从我的测试目录到%APPDATA%\npm的适当连接。
我还尝试更改代码,在回调中使用fs.readFile而不是fs.readFileSync来调用frisby,但仍然存在相同的问题。
如前所述,我的其他测试和Frisby附带的示例运行并返回,没有错误。具体来说,httpbin_binary_post_put_spec.js示例使用了与我自己编写的代码几乎相同的代码,而且该示例工作得很好。
我已经通过Fiddler路由了HTTP请求,可以看到请求和响应,在那里一切看起来都很好。它获得一个HTTP 200,响应体具有我希望与文件内容进行比较的预期JSON。
为什么我会收到这个关于未定义对象的错误?
发布于 2014-07-16 21:58:27
解决橡胶鸭子问题似乎把我从自己的愚蠢中救了出来。
需要将字符串转换为适当的JSON对象:
.expectJSON(getJSON) -> .expectJSON(JSON.parse(getJSON))
https://stackoverflow.com/questions/24772356
复制相似问题