gpt4 book ai didi

typescript - 使用 `user` 截断 meteor `userId` 和 `sinon-ts`

转载 作者:行者123 更新时间:2023-12-04 07:55:01 27 4
gpt4 key购买 nike

我要 stub useruserId使用 sinon-ts这样我就可以测试从数据库中获取结果的服务器端代码。
如果我使用普通 sinon我可以 stub useruserId正确并且测试通过。虽然通过了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尝试一下,这样我就不会显示任何语法错误。我似乎无法让它 stub useruserId正确。
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)
我花了很多时间环顾四周,但找不到任何关于 stubing Meteor 的人 useruserId使用 sinon-ts .
达到相同结果的正确方法是什么?
更新
使用 callFake 抛出异常
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)

最佳答案

原来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/

关于typescript - 使用 `user` 截断 meteor `userId` 和 `sinon-ts`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66743972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com