Nepal on Rails

millisami's rants!

Stubbing Network Calls (Api) Using Dyson for Emberjs Apps

Testing emberjs apps is the current buzzing topic. Here I try out my way to stub network calls while doing emberjs integration tests.

There are many libraries popping out in this field. e.g. ember-testing-httpRespond, jquery-mockjax, etc.

Here I would like to try out dyson. Its a nodejs app with simple DSL. Though it has its fake data generator dyson-generators, but I find that it has limited generators. So, for fake data generation, we’ll be using Faker which has massive amounts of fake contextual data.

Lets whip up one for our next ambitious fake API.

Clone the repo

We’ll use the dyson-demo repo for our own api.

git clone git@github.com:webpro/dyson-demo.git articles-api
cd articles-api
npm install
npm install Faker --save
echo 'node_modules/' > .gitignore
git add .gitignore
git commit -m 'node_modules folder git ignored'

Start the server with npm start. It starts the server on port 3000 by default, which can be customized in file dyson-demo.js

Visit http:localhost:3000 to list the stubbed endpoints. You will see some demo end points which you can wipe it out or keep.

Setup our API endpoint

Here, we will use Article as our example resource. To list the articles end point in the browser, first we’ll have to make an entry in the file services/get/_index.js

1
2
3
4
5
var examples = [
  '/articles',
  ....
  ....
]

Now we have to add a template for articles. Create the file.

services/get/articles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var g = require('dyson-generators');
var f = require('Faker');
var currentId = 0;

var articles = {
  path: '/articles',
  collection: true,
  size: 30,
  template: {
    id: function() {
      currentId += 1;
      return currentId.toString();
    },
    title: function() {
      return "Article #" + this.id;
    },
    body: f.Lorem.paragraphs()
  },
  container: {
    articles: function(params, query, data) {
        return data;
    }
  }
};

module.exports = [articles];

For dyson to pick up the changes, we should re-start the server. Then if we click the /articles or visit http://localhost:3000/articles, the output should look like the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "articles": [
    {
      "id": "1",
      "title": "Article #1",
      "body": "iure consequuntur numquam fugit inventore repudiandae\nfacere qui vel aut veniam eaque dolore incidunt\nnam vero rem expedita qui corporis voluptatum fuga distinctio\nomnis nihil et\n \r\tquam ipsam similique perferendis\nvoluptas ad voluptatem ut voluptatibus quia eius\nreprehenderit commodi repellendus iste\naccusamus error omnis asperiores qui\nqui distinctio quidem modi ullam quia voluptatem\n \r\tharum quia modi officia ea odio autem\nomnis reprehenderit autem molestiae\nquidem numquam iusto voluptatem"
    },
    {
      "id": "2",
      "title": "Article #2",
      "body": "iure consequuntur numquam fugit inventore repudiandae\nfacere qui vel aut veniam eaque dolore incidunt\nnam vero rem expedita qui corporis voluptatum fuga distinctio\nomnis nihil et\n \r\tquam ipsam similique perferendis\nvoluptas ad voluptatem ut voluptatibus quia eius\nreprehenderit commodi repellendus iste\naccusamus error omnis asperiores qui\nqui distinctio quidem modi ullam quia voluptatem\n \r\tharum quia modi officia ea odio autem\nomnis reprehenderit autem molestiae\nquidem numquam iusto voluptatem"
    },
    ........
    ........
    {
      "id": "30",
      "title": "Article #30",
      "body": "iure consequuntur numquam fugit inventore repudiandae\nfacere qui vel aut veniam eaque dolore incidunt\nnam vero rem expedita qui corporis voluptatum fuga distinctio\nomnis nihil et\n \r\tquam ipsam similique perferendis\nvoluptas ad voluptatem ut voluptatibus quia eius\nreprehenderit commodi repellendus iste\naccusamus error omnis asperiores qui\nqui distinctio quidem modi ullam quia voluptatem\n \r\tharum quia modi officia ea odio autem\nomnis reprehenderit autem molestiae\nquidem numquam iusto voluptatem"
    }
  ]
}

So, now we got our api that responds to /articles. To get better understanding of dyson, you can look out the examples that it already exists in the repo. You can also see the README of Faker.js to find various fake data generators.

I’ve pushed the repo at https://github.com/millisami/articles-api for future reference.

In the next part, we’ll write an Ember app integration testing using this api.

Comments