Setting up Trek's Pretender for Demo / Development


#1

I know this seems simplistic but I don’t see in the README for pretender where we are supposed to put our mock server code. Basically, where do we put the code below in an Ember-cli app?

var PHOTOS = {
  "10": {
    id: 10,
    src: 'http://media.giphy.com/media/UdqUo8xvEcvgA/giphy.gif'
  },
  "42": {
    id: 42,
    src: 'http://media0.giphy.com/media/Ko2pyD26RdYRi/giphy.gif'
  }
};

var server = new Pretender(function(){
  this.get('/photos', function(request){
    var all =  JSON.stringify(Object.keys(PHOTOS).map(function(k){return PHOTOS[k]}))
    return [200, {"Content-Type": "application/json"}, all]
  });

  this.get('/photos/:id', function(request){
    return [200, {"Content-Type": "application/json"}, JSON.stringify(PHOTOS[request.params.id])]
  });
});

I want to run the mock server for development and demo purposes not just for testing. I know I am missing something simple but I do not see any explanation on how to do this.


#2

+1. I’m using ember-cli-pretender and can’t find very much information on how to setup it up (where to place the server config, etc.). Any help would be much appreciated.


#3

@brendanoh, @BradWheel Did you ever figure out a good way to do this?


#4

How is that different from the http-mock in ember-cli?


#5

Just guessing here but I think part of the difference is that Pretender intercepts your API calls. Where as a http-mock is really just an express server setup to run as your stubbed API so doing real AJAX behind the scenes. End effect is mostly the same. But the Pretender stuff is typically more tightly coupled to test cases such as integration tests testing very specific models and pages.

With http-mock it seems like you could build out a more extensive virtual API layer if you were so inclined.

For full on demo I personally would use http-mock. But write my tests with Pretender.