Reputation: 442
import Application from 'appkit/app';
import Router from 'appkit/router';
function startApp(attrs) {
var App;
var attributes = Ember.merge({
// useful Test defaults
rootElement: '#ember-testing',
// LOG_ACTIVE_GENERATION:false,
// LOG_VIEW_LOOKUPS: false
LOG_ACTIVE_GENERATION: true,
LOG_MODULE_RESOLVER: true,
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true,
LOG_VIEW_LOOKUPS: true
}, attrs); // but you can override;
Ember.run.join(function(){
App = Application.create(attributes);
App.setupForTesting();
App.injectTestHelpers();
});
Router.reopen({
location: 'none'
});
App.reset(); // this shouldn't be needed, i want to be able to "start an app at a specific URL"
return App;
}
export default startApp;
//Question : I would like to override default application:adapter which is pointing to Rest //adapter in my real application with Fixture adapter I tried app.register method but its not //working any pointer.
Upvotes: 1
Views: 472
Reputation: 7858
I have found the way around this -- however, I'll include a disclaimer at the bottom.
In order to use the Fixture adapter when your application is already configured to use another adapter, you need to redefine your application adapter so that it uses the DS.FixtureAdapter
class.
I see that you're using EAK, so I have included this code into the startApp function, just before the Ember.run
function that creates the app.
define('myApp/adapters/application', ['exports'], function(exports) {
exports['default'] = DS.FixtureAdapter;
});
Now you also need to define the fixtures for each of the models that your application has. You will likely not want to modify the actual source files because these are only for the acceptance tests, so redefining the classes does the trick as well.
I had created another helper called setFixtures:
function setFixtures(modelName, fixtures) {
var model = require('myApp/models/' + modelName)['default'];
model.reopenClass({
FIXTURES: fixtures
});
}
export default setFixtures;
After making jshint happy by adding this as a global function, and adding the following into test-helper.js
:
window.setFixtures = require('myApp/tests/helpers/set-fixtures')['default'];
I am now ready to use this in each of my modules for testing:
module('Acceptances - My fixtures', {
setup: function() {
setFixtures('my-model', [
{ id: 1, name: 'John Madden' },
{ id: 2, name: 'AEIOUAEIOUAEIOU' }
]);
App = startApp();
}
});
// your tests below
Disclaimer: I walked this lonely road, the only one I had ever known, but it seems that DS.FixtureAdapter
has still some issues (at the moment of writing), plus it has several differences with what you would expect with DS.RESTAdapter
(more info here). However, most of this you can overcome by adjusting how your fixtures look like on the tests.
The biggest problem is around FixtureAdapter and hasMany relationships, specially with those that are async. There's a lot of information in the GitHub issues section of ember data and of course that they will get resolved in time, but at the moment, it is quite flaky.
I did reach a point where I forcefully would have had to modify the source code so that FixtureAdapter would be happy, or redefining all model classes on the fly for the test -- which seemed to me like rewriting part of the app for the tests. That prevents me from testing lots of issues that could be actual bugs (e.g. forgot an async relationship? Well, acceptance tests are not going to warn you since you redefined the classes to not need them.)
I'm now leaning towards Ember-testing-httpRespond. However, that's material for other questions.
Upvotes: 1