[英]How to spy on a Falcor Data Model constructor in Jasmine 1
I am trying to mock the constructor returned by require('falcor');
我试图模拟
require('falcor');
返回的构造函数require('falcor');
I have two routes and one calls the other route using var dataModel = new falcor({source: this});
我有两条路线,一条通过
var dataModel = new falcor({source: this});
调用另一条路线var dataModel = new falcor({source: this});
Code looks like so 代码看起来像这样
var falcor = require('falcor');
module.exports = {
route: 'items',
get: function (pathSet) {
var dataModel = new falcor({source: this});
var ids = '1';
dataModel.get('itemIds', ids).then(function (response) {
// Code I can't get to in Jasmine 1.x tests
});
}
}
I want the constructor to return a spy so I can call Promise.resolve
and send back mock data for testing purposes. 我希望构造函数返回一个间谍,因此我可以调用
Promise.resolve
并发送回模拟数据以进行测试。 I'm not sure how to do this without moving the call into another module that I can mock separately. 我不确定如何在不将调用移到我可以单独模拟的另一个模块中的情况下执行此操作。 I think some questions that may help me here are
我认为有些问题可能对我有帮助
spyOn(global, 'falcor').andReturn(/* object with a mocked get method*/);
spyOn(global, 'falcor').andReturn(/* object with a mocked get method*/);
? Thanks for any help. 谢谢你的帮助。
To start w/ question 2: yes, to get data from another route, return refs to that route. 从问题2开始:是的,要从其他路由获取数据,请将引用返回该路由。 Don't instantiate another model w/i the route.
不要使用该路线实例化其他模型。 Eg
例如
const itemsRoute = {
route: 'items[{keys:indices}]',
get(pathSet) {
// map indices to item ids, likely via DB call
// in this case, SomeDataModel handles network requests to your data store and returns a promise
return SomeDataModel.getItemsByIndices(pathSet.indices)
.then(ids => ids.map((id, idx) => ({
path: ['items', pathSet.indices[idx]],
value: {
$type: 'ref',
value: ['itemById', id]
}
})));
}
};
const itemByIdRoute = {
route: 'itemById[{keys:ids}].name',
get(pathSet) {
return new Promise((resolve) => {
resolve(pathSet.idx.map(id => ({
path: ['itemById', id, 'name'],
value: {
$type: 'atom',
value: `I am item w/ id ${id}`
}
})));
});
}
};
When a request comes in for (eg) [items, 2, name]
, it will hit the first items
route, resolve [items, 2]
to [itemById, someId]
, and resolve the remaining name
key in the itemsById
route. 当请求(例如)
[items, 2, name]
,它将打到第一个items
路由,将[items, 2]
解析为[itemById, someId]
,并解决itemsById
路由中的其余name
键。
As for question 1: rather than mocking falcor, just mock whatever you are using to make the remote call to your data source. 对于问题1:与其模拟falcor,不如模拟用于远程调用数据源的任何东西。 In the above case, just mock
SomeDataModel
在上述情况下,只需模拟
SomeDataModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.