在 Meteor velocity test 中获取 Mongo 合集
Get Mongo Collection in Meteor velocity test
目前我正在做一个流星项目,我在里面写了一些包。现在我在为这些包编写测试代码时遇到了问题。
我在 Velocity 中使用了 mike:mocha。
在包中,我使用以下方法创建了一个集合:
TestCollect = new Meteor.Collection("testCollect");
在我的 package/server.js
if(TestCollect.find().count == 0){
TestCollect.insert({});
}
... /*I want to make sure there is only doc in this collection */
然后我在package.js
中导出这个变量
api.export("TestCollect");
在我的服务器测试代码中,它是这样的:
it("should contain only one doc", function(){
console.log(TestCollect.find());
chai.assert.equal(1, TestCollect.find().count(), "but it has " + TestCollect.find().count() + " docs");
});
令我惊讶的是,测试代码中的数据库似乎与我的包中的数据库完全不同。我猜 velocity 使用镜像 mongo 来运行测试代码。
其实就是console.log(TestCollect.find());在我的测试代码中返回:
I20150102-08:44:13.285(8)? [velocity-mirror] { _mongo:
I20150102-08:44:13.285(8)? { _connectCallbacks: [ [Function] ],
I20150102-08:44:13.285(8)? _observeMultiplexers: {},
I20150102-08:44:13.285(8)? _onFailoverHook: { nextCallbackId: 0, callbacks: {} },
I20150102-08:44:13.286(8)? _docFetcher: { _mongoConnection: [Circular], _callbacksForCacheKey: {} },
I20150102-08:44:13.286(8)? _oplogHandle:
I20150102-08:44:13.286(8)? { _oplogUrl: 'mongodb://127.0.0.1:3001/local',
I20150102-08:44:13.286(8)? _dbName: 'mocha',
I20150102-08:44:13.286(8)? _oplogLastEntryConnection: [Object],
I20150102-08:44:13.286(8)? _oplogTailConnection: [Object],
I20150102-08:44:13.286(8)? _stopped: false,
I20150102-08:44:13.286(8)? _tailHandle: [Object],
I20150102-08:44:13.287(8)? _readyFuture: [Object],
I20150102-08:44:13.287(8)? _crossbar: [Object],
I20150102-08:44:13.287(8)? _lastProcessedTS: [Object],
I20150102-08:44:13.287(8)? _baseOplogSelector: [Object],
I20150102-08:44:13.287(8)? _catchingUpFutures: [] },
I20150102-08:44:13.287(8)? db:
I20150102-08:44:13.287(8)? { domain: null,
I20150102-08:44:13.288(8)? _events: {},
I20150102-08:44:13.288(8)? _maxListeners: 10,
I20150102-08:44:13.288(8)? databaseName: 'mocha',
I20150102-08:44:13.288(8)? serverConfig: [Object],
I20150102-08:44:13.288(8)? options: [Object],
I20150102-08:44:13.288(8)? _applicationClosed: false,
I20150102-08:44:13.288(8)? slaveOk: false,
I20150102-08:44:13.289(8)? bufferMaxEntries: -1,
I20150102-08:44:13.289(8)? native_parser: false,
I20150102-08:44:13.289(8)? bsonLib: [Object],
I20150102-08:44:13.289(8)? bson: [Object],
I20150102-08:44:13.289(8)? bson_deserializer: [Object],
I20150102-08:44:13.289(8)? bson_serializer: [Object],
I20150102-08:44:13.289(8)? _state: 'connected',
I20150102-08:44:13.290(8)? pkFactory: [Object],
I20150102-08:44:13.290(8)? forceServerObjectId: false,
I20150102-08:44:13.290(8)? safe: false,
I20150102-08:44:13.290(8)? notReplied: {},
I20150102-08:44:13.291(8)? isInitializing: true,
I20150102-08:44:13.291(8)? openCalled: true,
I20150102-08:44:13.291(8)? commands: [],
I20150102-08:44:13.291(8)? logger: [Object],
I20150102-08:44:13.291(8)? tag: 1420159452700,
I20150102-08:44:13.292(8)? eventHandlers: [Object],
I20150102-08:44:13.292(8)? serializeFunctions: false,
I20150102-08:44:13.292(8)? raw: false,
I20150102-08:44:13.292(8)? recordQueryStats: false,
I20150102-08:44:13.292(8)? retryMiliSeconds: 1000,
I20150102-08:44:13.292(8)? numberOfRetries: 60,
I20150102-08:44:13.293(8)? readPreference: [Object] },
I20150102-08:44:13.293(8)? _primary: '127.0.0.1:3001' },
I20150102-08:44:13.293(8)? _cursorDescription:
I20150102-08:44:13.293(8)? { collectionName: 'kliuStatus',
I20150102-08:44:13.293(8)? selector: {},
I20150102-08:44:13.293(8)? options: { transform: null } },
I20150102-08:44:13.293(8)? _synchronousCursor: null }
那我应该如何让这个测试代码工作呢?有什么方法可以连接到"real" mongo?
您在镜像内测试 运行 并在不同的数据库上进行测试是正确的。
您在每个测试中应该做的是设置该测试所需的数据库。因此,当您编写测试时,您应该执行一些与此类似的步骤(未经测试的代码):
describe('the suite'), function() {
beforeEach(function() {
myCollection.remove({});
});
it('should add something to some document', function() {
// SETUP
myCollection.insert({some: "document"});
// EXECUTE
myCode.addSomething({hello: "world"});
// VERIFY
myDoc = myCollection.find({some: "document"});
assert(myDoc.hello === "world");
}
});
你可以在 mocha 中看到更多关于钩子的信息 here
https://github.com/Sanjo/meteor-jasmine/blob/master/specs/example.js
我有一个非常相似的问题,当我意识到我没有使用 Meteor.subscribe 时就解决了。我怀疑如果您将以下代码添加到您的客户端:Meteor.subscribe("TestCollect")
mike:mocha 将正常工作。
目前我正在做一个流星项目,我在里面写了一些包。现在我在为这些包编写测试代码时遇到了问题。 我在 Velocity 中使用了 mike:mocha。
在包中,我使用以下方法创建了一个集合:
TestCollect = new Meteor.Collection("testCollect");
在我的 package/server.js
if(TestCollect.find().count == 0){
TestCollect.insert({});
}
... /*I want to make sure there is only doc in this collection */
然后我在package.js
中导出这个变量 api.export("TestCollect");
在我的服务器测试代码中,它是这样的:
it("should contain only one doc", function(){
console.log(TestCollect.find());
chai.assert.equal(1, TestCollect.find().count(), "but it has " + TestCollect.find().count() + " docs");
});
令我惊讶的是,测试代码中的数据库似乎与我的包中的数据库完全不同。我猜 velocity 使用镜像 mongo 来运行测试代码。
其实就是console.log(TestCollect.find());在我的测试代码中返回:
I20150102-08:44:13.285(8)? [velocity-mirror] { _mongo:
I20150102-08:44:13.285(8)? { _connectCallbacks: [ [Function] ],
I20150102-08:44:13.285(8)? _observeMultiplexers: {},
I20150102-08:44:13.285(8)? _onFailoverHook: { nextCallbackId: 0, callbacks: {} },
I20150102-08:44:13.286(8)? _docFetcher: { _mongoConnection: [Circular], _callbacksForCacheKey: {} },
I20150102-08:44:13.286(8)? _oplogHandle:
I20150102-08:44:13.286(8)? { _oplogUrl: 'mongodb://127.0.0.1:3001/local',
I20150102-08:44:13.286(8)? _dbName: 'mocha',
I20150102-08:44:13.286(8)? _oplogLastEntryConnection: [Object],
I20150102-08:44:13.286(8)? _oplogTailConnection: [Object],
I20150102-08:44:13.286(8)? _stopped: false,
I20150102-08:44:13.286(8)? _tailHandle: [Object],
I20150102-08:44:13.287(8)? _readyFuture: [Object],
I20150102-08:44:13.287(8)? _crossbar: [Object],
I20150102-08:44:13.287(8)? _lastProcessedTS: [Object],
I20150102-08:44:13.287(8)? _baseOplogSelector: [Object],
I20150102-08:44:13.287(8)? _catchingUpFutures: [] },
I20150102-08:44:13.287(8)? db:
I20150102-08:44:13.287(8)? { domain: null,
I20150102-08:44:13.288(8)? _events: {},
I20150102-08:44:13.288(8)? _maxListeners: 10,
I20150102-08:44:13.288(8)? databaseName: 'mocha',
I20150102-08:44:13.288(8)? serverConfig: [Object],
I20150102-08:44:13.288(8)? options: [Object],
I20150102-08:44:13.288(8)? _applicationClosed: false,
I20150102-08:44:13.288(8)? slaveOk: false,
I20150102-08:44:13.289(8)? bufferMaxEntries: -1,
I20150102-08:44:13.289(8)? native_parser: false,
I20150102-08:44:13.289(8)? bsonLib: [Object],
I20150102-08:44:13.289(8)? bson: [Object],
I20150102-08:44:13.289(8)? bson_deserializer: [Object],
I20150102-08:44:13.289(8)? bson_serializer: [Object],
I20150102-08:44:13.289(8)? _state: 'connected',
I20150102-08:44:13.290(8)? pkFactory: [Object],
I20150102-08:44:13.290(8)? forceServerObjectId: false,
I20150102-08:44:13.290(8)? safe: false,
I20150102-08:44:13.290(8)? notReplied: {},
I20150102-08:44:13.291(8)? isInitializing: true,
I20150102-08:44:13.291(8)? openCalled: true,
I20150102-08:44:13.291(8)? commands: [],
I20150102-08:44:13.291(8)? logger: [Object],
I20150102-08:44:13.291(8)? tag: 1420159452700,
I20150102-08:44:13.292(8)? eventHandlers: [Object],
I20150102-08:44:13.292(8)? serializeFunctions: false,
I20150102-08:44:13.292(8)? raw: false,
I20150102-08:44:13.292(8)? recordQueryStats: false,
I20150102-08:44:13.292(8)? retryMiliSeconds: 1000,
I20150102-08:44:13.292(8)? numberOfRetries: 60,
I20150102-08:44:13.293(8)? readPreference: [Object] },
I20150102-08:44:13.293(8)? _primary: '127.0.0.1:3001' },
I20150102-08:44:13.293(8)? _cursorDescription:
I20150102-08:44:13.293(8)? { collectionName: 'kliuStatus',
I20150102-08:44:13.293(8)? selector: {},
I20150102-08:44:13.293(8)? options: { transform: null } },
I20150102-08:44:13.293(8)? _synchronousCursor: null }
那我应该如何让这个测试代码工作呢?有什么方法可以连接到"real" mongo?
您在镜像内测试 运行 并在不同的数据库上进行测试是正确的。
您在每个测试中应该做的是设置该测试所需的数据库。因此,当您编写测试时,您应该执行一些与此类似的步骤(未经测试的代码):
describe('the suite'), function() {
beforeEach(function() {
myCollection.remove({});
});
it('should add something to some document', function() {
// SETUP
myCollection.insert({some: "document"});
// EXECUTE
myCode.addSomething({hello: "world"});
// VERIFY
myDoc = myCollection.find({some: "document"});
assert(myDoc.hello === "world");
}
});
你可以在 mocha 中看到更多关于钩子的信息 here
https://github.com/Sanjo/meteor-jasmine/blob/master/specs/example.js
我有一个非常相似的问题,当我意识到我没有使用 Meteor.subscribe 时就解决了。我怀疑如果您将以下代码添加到您的客户端:Meteor.subscribe("TestCollect")
mike:mocha 将正常工作。