Gruntjs 为yeoman生成的应用程序中的API调用提供存根

Gruntjs 为yeoman生成的应用程序中的API调用提供存根,gruntjs,yeoman,stubbing,Gruntjs,Yeoman,Stubbing,我一直在尝试为使用yeoman生成的应用程序创建api存根。这里没什么特别的,我只是打电话: mkdir demo1 cd demo1 yo angular 出于开发目的,我需要api存根,到目前为止,我找到的唯一一个模块就是api存根。然而,据我所知,lineman并不十分友好,因此我在端口8000上启动了lineman,提供了一些api存根(如文档中的存根),并在项目节点模块中添加了“grunt connect proxy”:“~0.1.5”,然后创建一个代理,将所有localhost:

我一直在尝试为使用
yeoman
生成的应用程序创建api存根。这里没什么特别的,我只是打电话:

mkdir demo1 
cd demo1
yo angular
出于开发目的,我需要api存根,到目前为止,我找到的唯一一个模块就是api存根。然而,据我所知,lineman并不十分友好,因此我在端口
8000
上启动了lineman,提供了一些api存根(如文档中的存根),并在项目节点模块中添加了
“grunt connect proxy”:“~0.1.5”
,然后创建一个代理,将所有
localhost:9000/api/*
传递到
localhost:8000/api/*
,从而提供存根

虽然这样做有效,但我想删除lineman依赖项,并自己提供存根。这是我到目前为止编写的代码(添加了
express
作为路由/参数解析的依赖项)-大多数代码都是来自express模块的比特和片段,以便将
http.ServerRequest
http.ServerResponse
包装到express
request
response

// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

var drawRoutes = require('./config/server').drawRoutes;
var express = require('express');
var expressApp = express();
drawRoutes(expressApp);
var router = expressApp._router;

// Provide api stubs through express router functionality.
var apiStubSnippet = function(req, res, next) {
  var match = router.matchRequest(req);
  if (typeof(match) !== 'undefined') {
    // Found a match, invoke it
    if (match.callbacks.length > 0) {

      var _req = require('express/lib/request');
      var _resp = require('express/lib/response');

      // Set the two objects together ... but why?!
      _req.__proto__ = req;
      _req.app = expressApp;
      _req.res = _resp;

      _resp.__proto__ = res;
      _resp.app = expressApp;
      _resp.req = _req;

      match.callbacks[0](_req, _resp);
    }
  } else {
    // No match on the router, go next.
    next();
  }
};
此时,调用似乎已被解决(控制台中不再有错误)。但是,电话一直挂着,我不知道如何继续。我很确定代码对于它需要做的事情来说过于复杂,但是我是grunt和node模块的初学者


任何帮助都可以,谢谢。

Angular允许您使用
$httpBackend
存根,您尝试过这个吗

我喜欢尽可能打破JS最佳实践,因此我在今年早些时候为一个项目做了如下工作:

app.run(function ($httpBackend) {
  var createResponse = function (type, url, status, response) {
    var when = $httpBackend.when(type, url);
    if (angular.isDefined(status) && angular.isDefined(response))
      when.respond(status, response);
    return when;
  };

  String.prototype.get =
  RegExp.prototype.get = function (status, response) { return createResponse('GET', this, status, response); };

  String.prototype.post =
  RegExp.prototype.post = function (status, response) { return createResponse('POST', this, status, response); };

  String.prototype.put =
  RegExp.prototype.put = function (status, response) { return createResponse('PUT', this, status, response); };

  /^\/views\//
    .get()
    .passThrough();

  '/api/login'
    .post(200, {
      // response object.
    });
});

此特定方法可能不是您想要的,但可能是。

这两个端口是为同一上下文配置的,请尝试更改为:localhost:9000/*和localhost:8000/api/*。
这些上下文和端口是在Grunfile中配置的?

感谢您的回复。我真正想要的是不改变任何客户端代码。当然,您可以在服务/控制器对象中模拟xhr,提供一个不同的服务来模拟真实的东西(这正是您在测试中所做的)。我仍然需要在这里找到解决方案:)。如果我解决了问题,将发布。您只在开发中包含$httpBackend模块。它是由Angular公司为您的目的设计的。我不知道为什么您希望在堆栈的更上层经历许多麻烦以产生相同的结果。我不希望更改客户端应用程序中的一行代码,而是存根底层API实现(即后端)。仍然没有找到解决方案,但当我解决它时会在这里发布(如果有)。