简体   繁体   English

如何在Jasmine 1中监视Falcor数据模型构造函数

[英]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 我认为有些问题可能对我有帮助

  1. Where do I find the constructor functions defined by modules like falcor? 在哪里可以找到由falcor等模块定义的构造函数? I have tried looking into the 'global' object but have had no luck. 我尝试调查“全局”对象,但没有运气。 If I did find this constructor, could I just replace it with a spyOn(global, 'falcor').andReturn(/* object with a mocked get method*/); 如果确实找到了此构造函数,是否可以将其替换为spyOn(global, 'falcor').andReturn(/* object with a mocked get method*/); ?
  2. Is there a better way that makes testing easier to call a route from inside another route? 有没有更好的方法可以使测试更容易从另一个路由内部调用一个路由?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM