Stub factories for ember-cli


#1

Hi,

We’re using https://github.com/danielspaniel/ember-data-factory-guy for testing fixtures and right now I’m in the tedious process of writing API stubs for all our models (for local development). Is anyone aware of a decent mock/stub library on the server side?

If not, I might give it a shot to build an addon for this…


#2

Replying to my own thread with a small update… I’m working on a little lib for this now which will allow you to use a ember-data-esque syntax to define stubs in your http-mock routes… We’re already using stub libs during testing but during development we want to have a working API with stubs and there was just no easy way to do it…

So far, the syntax looks like:

  var Store = require('../stubber');
  var store = new Store();

  store.define('user', {
    name: store.attr('string', {words:2})
  });

  store.define('post', {
    body: store.attr('string',{words: 300}), // users faker.lorem
    views: store.attr('number'), // default is 0-200
    user: store.belongsTo('user'),
  });

  store.create('user',function(){}); // Not neccasary
  store.create('post',function(){}); 

  var postsRouter = express.Router();

  postsRouter.get('/:id', function(req, res) {
    res.send(store.find('posts', req.param('id')).serialize())
  });

  postsRouter.get('/', function(req, res) {
    res.send(store.find('posts').serialize()) //All
  });

  app.use('/api/posts', postsRouter);

It basically stores all records in memory in node which is sufficient for stubbing. Data won’t survive a server restart but that’s actually a good thing during development…

It follows ember-data’s pattern of a separation of store/adapter/serializer so using your own serialization logic should work pretty well…

Will probably put the thing on github later this week… I’m thinking to ship this as an addon but I’m not sure server addons are actually possible now. Are they @rwjblue ?

Edit: just checked and according to http://reefpoints.dockyard.com/2014/06/24/introducing_ember_cli_addons.html that should work just fine :slight_smile:

Providing custom express middlewares. This allows for an addon to completely customize the development servers behaviors, making things like automated mock Ember Data API’s actually possible. This is currently only available on master (will be available in 0.0.37 and higher).


#3

@joostdevries did you see https://github.com/dockyard/ember-cli-proxy-fixtures ?


#4

@abuiles No, looks awesome but it’s not really what I’m aiming for with this. Our API is under active development so often the front- and backend of a feature are developed simultaneously. This allows a ember dev to quickly mock the API and have a very basic level of functionality.

During testing, we use ember-data-factory-guy (so the fixtures are completely located in the frontend code).