Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Grunt to Mock Endpoints

I'm using Yeoman, Grunt, and Bower, to construct a platform for building a frontend independently of a a backend. The idea would be that all of my (AngularJS) controller, services, factories, etc live in this project, and get injected afterwards into my serverside codebase based off the result of grunt build.

My question is:

How can I mock endpoints so that the Grunt server responds to the same endpoints as my (Rails) App will?

At the moment I am using:

 angular.module('myApp', ['ngResource'])

 .run(['$rootScope', function ($rootScope) {
     $rootScope.testState = 'test';
  }]);

And then in each of my individual services:

   mockJSON = {'foo': 'myMockJSON'}

And on every method:

   if($rootScope.testState == 'test'){
    return mockJSON;
  }
  else {
    real service logic with $q/$http goes here
  }

Then after grunt build, testState = 'test' gets removed.

This is clearly a relatively janky architecture. How can I avoid it? How can I have Grunt respond to the same endpoints as my app (some of which have dynamic params) apply some logic (if necessary), and serve out a json file (possibly dependent on path params)?

like image 943
Abraham P Avatar asked Jul 29 '13 21:07

Abraham P


People also ask

Can I add multiple mocking rules for one endpoint?

It is possible to add multiple mocking rules for one endpoint. When a request comes, it is matched against the rules in the pre-determined order. The platform sends the mocked response according to the first rule that matches. It also supports sharing examples of requests and responses through unique links for collaboration with teammates.

How do I run grunt from a specific directory?

npm install -g grunt-cli This will put the grunt command in your system path, allowing it to be run from any directory. Note that installing grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile.

How does the grunt CLI work?

The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously. How the CLI works Each time grunt is run, it looks for a locally installed Grunt using node's require () system.

How do I run a grunt task without specifying a task?

You can configure Grunt to run one or more tasks by default by defining a default task. In the following example, running grunt at the command line without specifying a task will run the uglify task. This is functionally the same as explicitly running grunt uglify or even grunt default.


Video Answer


1 Answers

I've fixed this issue by using express to write a server that responds with static json.

First I created a directory in my project called 'api'. Within that directory I have the following files:

package.json:

   {
     "name": "mockAPI",
     "version": "0.0.0",
     "dependencies": {
        "express": "~3.3.4"
      }
   }

Then I run npm install in this directory.

index.js:

    module.exports = require('./lib/server');

lib/server.js:

    express = require('express');
    var app = express();

    app.get('/my/endpoint', function(req, res){
        res.json({'foo': 'myMockJSON'});
   });

    module.exports = app

and finally in my global Gruntfile.js:

         connect: {
            options: {
               port: 9000,
               hostname: 'localhost',
            },
            livereload: {
              options: {
                 middleware: function (connect, options) {
                   return [
                     lrSnippet,
                     mountFolder(connect, '.tmp'),
                     mountFolder(connect, yeomanConfig.app),
                     require('./api')
                   ];
               }
            }
         },

Then the services make the requests, and the express server serves the correct JSON.

After grunt build, the express server is simply replaced by a rails server.

like image 128
Abraham P Avatar answered Oct 04 '22 16:10

Abraham P