Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwriting application to use DS.FixtureAdapter in start-app.js #3249

Closed
corydobson opened this issue Feb 10, 2015 · 5 comments
Closed

Overwriting application to use DS.FixtureAdapter in start-app.js #3249

corydobson opened this issue Feb 10, 2015 · 5 comments

Comments

@corydobson
Copy link

start-app.js problems

Hi Ember-Devs,

I'm working on a project that is using the DS.RESTAdapter for production code, however I want to use the FixtureAdapter for my integration tests. Im having problems overwriting this property of the application in start-app.js using Ember-CLI and Ember-CLI-Mocha

import Ember from 'ember';
import DS from 'ember-data';
import Application from '../../app';
import Router from '../../router';
import config from '../../config/environment';

export default function startApp(attrs) {
  var application;

  var attributes = Ember.merge({}, config.APP);
  attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;

  Ember.run(function() {
    Application.ApplicationAdapter = DS.FixtureAdapter.extend();
    application = Application.create(attributes);
    application.setupForTesting();
    application.injectTestHelpers();
  });

  return application;
}

And the test

import Ember from "ember";
import startApp from '../../helpers/start-app';

var App;
describe('Integration Index Route', function() {
    before(function() {
      App = startApp();
    });

    afterEach(function() {
      App.reset();
    });

    it('Shows Content', function(done) {
      Ember.run(function(){
        visit('/').then(function(){
          expect(App.$('.content-box'));
          done();
        });
      });
    });
});

and the route

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return this.store.find('content');
  }
});

However Ember is still using the RESTAdapter calling my api endpoint as defined in the application adapter.

What am I doing wrong / not doing to overwrite this adapter for my tests?

@jbrown
Copy link
Contributor

jbrown commented Feb 10, 2015

This should be all you need...

// app/adapters/application.js
import config from '../config/environment';
import DS from 'ember-data';

var adapter;

if (config.environment === 'test') {
  adapter = DS.FixtureAdapter.extend();
} else {
  adapter = DS.RESTAdapter.extend();
}

export default adapter;

@corydobson
Copy link
Author

I feel that is bad practice, as now my main application depends on that variable.

I'd like to separate the test dependencies as much as possible. Is there a way to do this in start-app?

@jbrown
Copy link
Contributor

jbrown commented Feb 12, 2015

Honestly, I'd recommend not using the FixtureAdapter and just stick with the RESTAdapter for all environments. Use ember-data-factory-guy to create fixtures for the RESTAdapter.

@dgeb
Copy link
Member

dgeb commented Feb 12, 2015

@corydobson If you want to use the FixtureAdapter, you could register it on the application as follows in tests/helpers/start-app.js:

import Ember from 'ember';
import DS from 'ember-data';
import Application from '../../app';
import Router from '../../router';
import config from '../../config/environment';

export default function startApp(attrs) {
  var application;

  var attributes = Ember.merge({}, config.APP);
  attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;

  Ember.run(function() {
    application = Application.create(attributes);
    // register fixture adapter on the newly created application
    application.register('adapter:application', DS.FixtureAdapter);
    application.setupForTesting();
    application.injectTestHelpers();
  });

  return application;
}

With that said, I tend to agree with @jbrown and favor using a mock server library such as Pretender or Sinon for testing most applications. By mocking server responses, you can also test your adapters and serializers and better simulate error conditions.

For example, check out how Discourse uses Pretender to test authentication.

I don't have experience using ember-data-factory-guy, but that might be worth exploring too.

@corydobson
Copy link
Author

@dgeb Thanks for the help Dan!

Unfortunately using
// register fixture adapter on the newly created application application.register('adapter:application', DS.FixtureAdapter);

didn't work as expected either. I'll investigate using Pretender and go that route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants