Typescript 使用'sinon ts'删除流星'user'和'userId'`
我想使用Typescript 使用'sinon ts'删除流星'user'和'userId'`,typescript,meteor,sinon,Typescript,Meteor,Sinon,我想使用sinon ts存根user和userId,以便测试从数据库获取结果的服务器端代码 如果我使用纯sinon我可以正确地存根user和userId,并且测试通过。虽然它通过了Webstorm显示了返回的方法不存在的错误等,所以我认为它与Typescript的关系不太好 sinon-通过 import {Factory} from 'meteor/dburles:factory'; import {Meteor} from 'meteor/meteor'; import {resetData
sinon ts
存根user
和userId
,以便测试从数据库获取结果的服务器端代码
如果我使用纯sinon
我可以正确地存根user
和userId
,并且测试通过。虽然它通过了Webstorm
显示了返回的方法不存在的错误等,所以我认为它与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)
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)
错误
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)
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)
TypeError:sinon.stub(…).callsFake不是函数
胡克。(测试/主要测试:19:36)
运行时(包/测试:mocha-core/server.js:36:29)
事实证明,sinon ts
围绕sinon实现了“扩展”,以使其更好地使用TypeScript,但它仍然支持“默认”sinon行为
已知的可以与Meteor.user()和Meteor.userId()一起使用,并且可以通过
import sinon,{stubInterface}来自“ts sinon”;
const functionStub=sinon.stub();
或通过
从“ts sinon”导入*为tsSinon
const functionStub=tsSinon.default.stub();
将此方案应用于您当前的代码将产生以下代码,这些代码将与Meteor的用户功能一起使用:
从'Meteor/Meteor'导入{Meteor};
从“ts sinon”导入*作为sinon;
// ... 其他进口
描述(“测试”,功能(){
让当前用户;
在每个之前(()=>{
重置数据库();
Factory.define('user',Meteor.users{
});
currentUser=Factory.create('user');
sinon.default.stub(Meteor,'user').callsFake(()=>currentUser);
sinon.default.stub(Meteor,'userId').callsFake(()=>currentUser.\u id);
});
//…测试单元
});
参考资料:
错误表明函数没有存根,因为方法/发布调用检查是Meteor.userId
和Meteor.user
的一部分。是否需要将sinon.stubObject
与sinon ts
一起使用?你不能用默认的方式存根吗?sinon.stub(Meteor,'user')。callsFake(()=>currentUser)
?我使用普通的npm sinon包,我的用户存根使用这种模式。如果我使用callsFake,我会得到一个异常,因为找不到函数。我可以使用返回
,但这是一个突出显示的语法错误,可能与它是Typescript而不是Javascript有关。我已经更新了问题。我刚刚检查了文档,您应该可以通过sinon.default
,假设您当前的导入结构*为sinon
。ist是否使用sinon.default.stub(Meteor,'user').callsFake(()=>currentUser)
运行?这很有效,谢谢!我还将调用Meteor.user.restore
更改为userStub.restore
。你愿意回答这个问题吗?我会接受的。如果你愿意我回答,我非常乐意。