chai-fireproof
Chai 断言和 Firebase 和 Fireproof 的助手函数。
需求
你需要能够生成 Fireproof 引用。
使用
对象断言
首先像其他 Chai 插件一样加载插件:gulp.task(‘test:setup’, ‘设置测试。’, [‘build’], function() {
var Firebase = require(‘firebase’), chai = require(‘chai’);
require(‘./dist/chai-fireproof’); global.chai = chai; global.expect = chai.expect;
if (!process.env.FIREBASE_TEST_URL | !process.env.FIREBASE_TEST_SECRET) { |
gutil.log('Please set FIREBASE_TEST_URL and FIREBASE_TEST_SECRET.');
process.exit(1);
}
global.root = new Fireproof(new Firebase(process.env.FIREBASE_TEST_URL)); global.authToken = process.env.FIREBASE_TEST_SECRET;
});
chai.use(require('chai-fireproof'));
现在你可以像其他任何对象一样对 Fireproof 对象创建断言。注意,这些会返回 promise,你需要将其传回你的测试框架或自行处理。
Mocha 示例
describe('My Firebase', function() {
var root = new Fireproof(new Firebase('https://metropolis.firebaseio.com'));
it('should have some data in there already', function() {
return expect(root.child('robots')).to.exist;
});
it('should have some users in there', function() {
return expect(root.child('citizens')).to.deep.equal({
fred: {
name: 'Freder Frederson',
hometown: 'Metropolis',
assignment: 'Utopia'
},
maria: {
name: 'Maria',
hometown: 'Metropolis',
assignment: 'Underworld'
}
});
});
it('should have the water level in there', function() {
return expect(root.child('waterLevel')).to.be.lessThan(5);
})
});
安全测试
chai-fireproof 包含测试断言,用于验证安全规则是否按预期工作。注意,这些断言会返回 promise,你需要将其传回你的测试框架或自行处理。
安全测试有以下额外要求
- firebaseio-demo.com 不受支持,因为 Firebase 不会在那里检查规则。
- 在进行任何断言之前,请使用身份验证令牌调用
chai.setFirebaseAuthToken()
。
断言有四个新的标志和一个新方法
can
cannot
read
write
ref()
因此你可以编写与以下语法匹配的断言
return expect({ uid: 'metropolis:maria' }).can.read.ref(root.child('users/maria'));
return expect({ uid: 'metropolis:robotmaria'}).cannot.read.ref(root.child('users/maria'));
期望对象(即用断言包装的元素)是一个带有身份验证凭据的 Javascript 对象。对于写入测试,你可以提供一个对象来尝试写入 ref(用于测试验证规则等)。
return expect({ uid: 'metropolis:robotmaria'}).cannot.write(true)
.to.ref(root.child('city/agitation'));
以下规则适用
- 如果期望对象为
null
,则断言假设你指的是未经身份验证的用户。 - 除了
null
之外,每个用户对象都必须为uid
提供一个值。 - 要使令牌成为管理员令牌,请设置
admin: true
。注意,这个定义将导致任何测试通过。
文档
这里有 API 文档 here.
警告
为了捕获烦人的 Firebase 控制台消息,该库会对 console.log()
、console.warn()
和 console.error()
进行猴子补丁。你的里程可能会有所不同,在法律禁止的地方无效。