【问题标题】:Stubbing Meteor `user` and `userId` using `sinon-ts`使用 `sinon-ts` 存根 Meteor `user` 和 `userId`
【发布时间】:2021-06-18 23:24:25
【问题描述】:

我想使用sinon-ts 对user 和userId 进行存根,以便测试从数据库中获取结果的服务器端代码。

如果我使用普通的sinon,我可以正确地存根user 和userId,并且测试通过了。虽然它通过了Webstorm 显示了returns 方法不存在的错误等等,所以我认为它与Typescript 配合得不好。

sinon - 传球

import {Factory} from 'meteor/dburles:factory';
import {Meteor} from 'meteor/meteor';
import {resetDatabase} from 'meteor/xolvio:cleaner';
import {GiftListCollectionManager} from "../imports/api/collections/GiftListCollection";
import User = Meteor.User;
import { sinon } from 'meteor/practicalmeteor:sinon';

describe("Test", function () {
    beforeEach(() => {
        resetDatabase();
        Factory.define('user', Meteor.users, {

        });
        currentUser = Factory.create('user');
        sinon.stub(Meteor, 'user');

        Meteor.user.returns(currentUser);

        sinon.stub(Meteor, 'userId');

        Meteor.userId.returns(currentUser._id);
    });

    afterEach(() => {
        Meteor.user.restore();
        Meteor.userId.restore();
        resetDatabase();
    });

    it("Gets giftlists based on Meteor.userId()", () => {
        console.log("Gift lists")
        console.log(GiftListCollectionManager.getInstance().getGiftLists());
    })
}

我决定试一试sinon-ts,这样就不会出现语法错误。我似乎无法正确地存根 user 和 userId。

sinon-ts - 失败

import {Meteor} from 'meteor/meteor';
import {resetDatabase} from 'meteor/xolvio:cleaner';
import {GiftListCollectionManager} from "../imports/api/collections/GiftListCollection";

import * as sinon from "ts-sinon";

describe("Test", function () {

    let currentUser;

    beforeEach(() => {
        resetDatabase();
        Factory.define('user', Meteor.users, {

        });
        currentUser = Factory.create('user');

        const userStub = sinon.stubObject(Meteor);

        userStub.user.returns(currentUser);

        const userIdStub = sinon.stubObject(Meteor);

        userIdStub.userId.returns(currentUser._id);
    });


    it("Gets giftlists based on Meteor.userId()", () => {
        console.log("Gift lists")
        console.log(GiftListCollectionManager.getInstance().getGiftLists());
    })
});

错误

I20210322-09:45:44.170(0)?      Error: Meteor.userId can only be invoked in method calls or publications.
I20210322-09:45:44.170(0)?       at AccountsServer.userId (packages/accounts-base/accounts_server.js:117:13)
I20210322-09:45:44.171(0)?       at Object.Meteor.userId (packages/accounts-base/accounts_common.js:339:32)
I20210322-09:45:44.171(0)?       at GiftListCollectionManager.getGiftLists (imports/api/collections/GiftListCollection.ts:32:61)
I20210322-09:45:44.171(0)?       at Test.<anonymous> (tests/main.ts:66:61)
I20210322-09:45:44.171(0)?       at run (packages/meteortesting:mocha-core/server.js:36:29)
I20210322-09:45:44.171(0)?       at Context.wrappedFunction (packages/meteortesting:mocha-core/server.js:65:33)

我花了很多时间环顾四周,找不到任何关于使用 sinon-ts 对 Meteor user 和 userId 进行攻击的人。

达到相同结果的正确方法是什么?

更新

使用callsFake会抛出异常

import sinon = require('sinon');
sinon.stub(Meteor, 'user').callsFake(() => currentUser);
sinon.stub(Meteor, 'userId').callsFake(() => currentUser._id);

错误

TypeError: sinon.stub(...).callsFake is not a function
at Hook.<anonymous> (tests/main.ts:19:36)
at run (packages/meteortesting:mocha- core/server.js:36:29)

【问题讨论】:

  • 该错误表明该函数没有被存根,因为方法/发布调用检查是Meteor.userId 和Meteor.user 的一部分。您需要将sinon.stubObject 与sinon-ts 一起使用吗?你不能存根默认方式 sinon.stub(Meteor, 'user').callsFake(() =&gt; currentUser) 吗?我使用普通的 npm sinon 包,我的用户存根使用这种模式。
  • 如果我使用callsFake,我会得到一个异常,因为找不到该函数。我可以使用returns,但这被突出显示为语法错误,这可能与它是 Typescript 而不是 Javascript 有关。我已经更新了问题。
  • 我刚刚检查了文档,假设您当前的导入结构 * as sinon,您应该可以通过 sinon.default 访问 to access the underlying original sinon。那么 ist 是否使用sinon.default.stub(Meteor, 'user').callsFake(() =&gt; currentUser) 运行?
  • 成功了,谢谢!我还将呼叫Meteor.user.restore 更改为userStub.restore。你愿意回答这个问题,我会接受吗?
  • 如果您希望我回答,我非常乐意。

标签: typescript meteor sinon


【解决方案1】:

事实证明,sinon-ts 在 sinon 周围实现了“扩展”以使其更好地与 TypeScript 配合使用,但它仍然支持“默认”的 sinon 行为。

已知function stubs 可以与Meteor.user() 和Meteor.userId() 一起使用,并且可以通过以下方式访问

import sinon, { stubInterface } from "ts-sinon";

const functionStub = sinon.stub();

或通过

import * as tsSinon from "ts-sinon"

const functionStub = tsSinon.default.stub();

将此方案应用于您当前的代码将产生以下代码,预计可与 Meteor 的用户函数一起使用:

import {Meteor} from 'meteor/meteor';
import * as sinon from "ts-sinon";
// ... other imports

describe("Test", function () {
    let currentUser;

    beforeEach(() => {
        resetDatabase();
        Factory.define('user', Meteor.users, {

        });
        currentUser = Factory.create('user');

        sinon.default.stub(Meteor, 'user').callsFake(() => currentUser);
        sinon.default.stub(Meteor, 'userId').callsFake(() => currentUser._id);
    });

    // ... test units
});

参考资料:

https://github.com/ttarnowski/ts-sinon#sinon-methods

https://sinonjs.org/releases/v9.2.4/stubs/

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 2021-06-10
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2014-11-29
    • 2020-12-12
    • 1970-01-01
    相关资源
    最近更新 更多