Get Mongo Collection in Meteor velocity test

不打扰是莪最后的温柔 提交于 2019-12-12 03:19:52

问题


Currently I'm working on a meteor project, in which I wrote some packages. Now I have a problem when writing test codes for these packages. I used mike:mocha in Velocity.

In package I've created a Collection using:

    TestCollect = new Meteor.Collection("testCollect");

and in my package/server.js

    if(TestCollect.find().count == 0){
        TestCollect.insert({});
    }
    ... /*I want to make sure there is only doc in this collection */

Then I export this variable in package.js

    api.export("TestCollect");

and in my server test code, it's like:

    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");
        });

To my surprise, seems the db in the test code is totally different from the db in my packages. I guess velocity use a mirror mongo to run test codes.

In fact, the console.log(TestCollect.find()); in my test code returns:

    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 }

Then how should I get this test code to work? Any way to connect to the "real" mongo?


回答1:


You are right that tests run inside the mirror and do so on a different database.

What you should do in each test is setup the database you need for that test. So when you're writing a test you should perform some steps that are similar to this (untested code):

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");
    }

});

You can see more about hooks in mocha here

https://github.com/Sanjo/meteor-jasmine/blob/master/specs/example.js




回答2:


I had a very similar problem, and fixed in when I realized that I wasn't using Meteor.subscribe. I suspect if you add the following code to your client: Meteor.subscribe("TestCollect") mike:mocha will work correctly.



来源:https://stackoverflow.com/questions/27736268/get-mongo-collection-in-meteor-velocity-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!