为什么我不能测试文本文件的文本?

时间:2014-03-28 05:53:36

标签: node.js jasmine

我有一些方法可以在文本文件中设置文本。

在我运行测试之前,我已经设置了我的jasmine测试以创建具有错误值的文件。

var gpioPath = 'tests/gpio-test/class/gpio';
//setup the filesystem with the exports path
fs.outputFileSync(gpioPath+'/export',0);
for(var i=0;i<6;i++){
    fs.outputFileSync(gpioPath+i+'/value',2,'utf-8');
    fs.outputFileSync(gpioPath+i+'/direction','none','utf-8');
}

然后,我运行更新文件中的值的方法,然后检查文件现在是否具有正确的值。

  expect(fs.readFileSync(gpioPath+'/export','utf-8')).toBeGreaterThan(0);

        var pin1d = fs.readFileSync(gpioPath+'1/direction','utf-8');
        var pin1v = fs.readFileSync(gpioPath+'1/value','utf-8');
        expect(pin1d).toBe('out');
        expect(pin1v).toBe(0);

出于某种原因,这些测试都失败了。我得到了

'0' expected to be greater than '0'

但是当我查看文件时,我的值为6.

我已经尝试在运行测试之前放置一个waits,以防文件尚未更改,但即使等待10秒,这仍然是失败的。

我没有从fileWrite方法中获得任何错误,这是更新数据的内容,正如我所说,我可以看到文件已更新,为什么我的测试不能反映这一点? / p>

1 个答案:

答案 0 :(得分:0)

fs.readFileSync(gpioPath+'/export','utf-8')将返回一个字符串,因此您实际上正在执行if (0 > '0'),这是错误的。

如果您需要测试数字,则需要将其解析为数字:

expect(parseInt(fs.readFileSync(gpioPath+'/export','utf-8'), 10))
    .toBeGreaterThan(0);

这假设文件包含ascii零,而不是空字节。