Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 如何测试依赖meteor.user()的meteor方法_Testing_Meteor_Jasmine - Fatal编程技术网

Testing 如何测试依赖meteor.user()的meteor方法

Testing 如何测试依赖meteor.user()的meteor方法,testing,meteor,jasmine,Testing,Meteor,Jasmine,我正试图确定测试代码的最佳方法,但在我尝试过的各个方面都遇到了麻烦 基本代码是这样的(尽管由于实际上实现了这一点的“触发器”的多个层次而复杂得多): 客户端填充一个对象 客户端调用meteor方法并传递对象 Meteor方法使用Meteor.user()获取当前用户,向对象添加一个“createdby”属性,插入该对象,然后创建另一个对象(不同类型),该对象具有依赖于第一个对象的各种属性,以及数据库中已有的一系列其他内容 我试着用Velocity和Jasmine。我更愿意集成测试这些步骤来创建第

我正试图确定测试代码的最佳方法,但在我尝试过的各个方面都遇到了麻烦

基本代码是这样的(尽管由于实际上实现了这一点的“触发器”的多个层次而复杂得多):

  • 客户端填充一个对象
  • 客户端调用meteor方法并传递对象
  • Meteor方法使用Meteor.user()获取当前用户,向对象添加一个“createdby”属性,插入该对象,然后创建另一个对象(不同类型),该对象具有依赖于第一个对象的各种属性,以及数据库中已有的一系列其他内容
  • 我试着用Velocity和Jasmine。我更愿意集成测试这些步骤来创建第一个对象,然后测试第二个对象是否正确创建

    我的问题是,如果我在服务器上执行此操作,Meteor.user()调用将无法工作。如果我在客户机上这样做,我需要订阅大量的集合,以使逻辑正常工作,这似乎很困难


    有更好的方法吗?有没有办法在服务器集成测试中模拟或模拟用户登录?

    在jasmine测试中,您可以模拟调用Meteor.user(),如下所示:

    spyOn(Meteor, "user").and.callFake(function() {
        return 1234; // User id
    });
    

    您可能希望指定一个用户ID或根据已执行的测试更改日志状态。然后,我建议在测试项目中创建meteor方法:

    logIn = function(userId) {
        Meteor.call('logIn', userId);
    };
    
    logOut = function() {
        Meteor.call('logOut');
    }
    
    
    Meteor.userId = function() {
        return userId;
    };
    
    Meteor.user = function() {
        return userId ? {
            _id: userId,
            username: 'testme',
            emails: [{
                address: 'test@domain.com'
            }],
            profile: {
                name: 'Test'
            }
        } : null;
    };
    
    Meteor.methods({
        logIn: function(uid) {
            userId = uid || defaultUserId;
        },
        logOut: function() {
            userId = null;
        }
    });
    

    如果您不想仅在这个用例中依赖完整的库,您可以使用beforeach和afterEach轻松地模拟Meteor.urser():

    import {chai, assert} from 'meteor/practicalmeteor:chai';
    import {Meteor} from 'meteor/meteor';
    import {Random} from 'meteor/random';
    
    describe('user mocking', () => {
    
        let userId = null;
        let userFct = null;
    
        const isDefined = function (target) {
            assert.isNotNull(target, "unexpected null value");
            assert.isDefined(target, "unexpected undefined value");
    
            if (typeof target === 'string')
                assert.notEqual(target.trim(), "");
        };
    
        //------------------------------------------//
    
        beforeEach(() => {
    
            // save the original user fct
            userFct = Meteor.user;
    
            // Generate a real user, otherwise it is hard to test roles
            userId = Accounts.createUser({username: Random.id(5)});
            isDefined(userId);
    
            // mock the Meteor.user() function, so that it
            // always returns our new created user
            Meteor.user = function () {
                const users = Meteor.users.find({_id: userId}).fetch();
                if (!users || users.length > 1)
                    throw new Error("Meteor.user() mock cannot find user by userId.");
                return users[0];
            };
        });
    
        //------------------------------------------//
    
        afterEach(() => {
            //remove the user in the db
            Meteor.users.remove(userId);
            // restore user Meteor.user() function
            Meteor.user = userFct;
            // reset userId
            userId = null;
        });
    
        it("works...", () => {
            // try your methods which make use of 
            // Meteor.user() here
        });
    
    });
    

    它确保Meteor.user()只返回您在中创建的用户。这至少是一件好事,当你想测试所有其他东西并假设用户创建和Meteor.user()按预期工作时(这是模仿的本质,正如我目前所看到的)。

    太简单了!事实上,我必须返回一个带有_id作为属性的对象,而不仅仅是id(即{u id:'1234'),但除此之外,这是可行的。谢谢如何运行测试?