I am using Ember-cli and qunit for testing.
Item Model
import DS from 'ember-data';
var attr = DS.attr,
belongsTo = DS.belongsTo;
export default DS.Model.extend({
offer: belongsTo('offer'),
});
Here am adding test for relation between item and Offer model.
Item Tests
import Ember from "ember";
import DS from "ember-data";
import { test, moduleForModel } from 'ember-qunit';
moduleForModel('item', 'Item Model', {
needs: ['model:item']
});
test('offer relationship', function() {
var relationships = Ember.get(App.Item, 'relationships');
deepEqual(relationships.get(App.Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});
Error Trace:
Died on test #1 at test (http://localhost:4200/assets/vendor.js:73836:13)
at eval (goodcity/tests/unit/item-test.js:44:5)
at requireModule (http://localhost:4200/assets/vendor.js:54:29)
at http://localhost:4200/assets/test-loader.js:14:29: App is not defined
Source:
ReferenceError: App is not defined
at Object.eval (goodcity/tests/unit/item-test.js:45:37)
at Object.wrapper (http://localhost:4200/assets/vendor.js:73824:31)
at Object.Test.run (http://localhost:4200/assets/qunit.js:203:18)
at http://localhost:4200/assets/qunit.js:361:10
at process (http://localhost:4200/assets/qunit.js:1453:24)
at http://localhost:4200/assets/qunit.js:479:5
Am i missing something?
I'm just now in the process of converting over an older Ember App to the new ember-cli and ran into a similar situation. Since Ember CLI uses ES6 module syntax you can't access anything directly on the App object.
You will need to import your objects from their respective model files.
import Item from "<modulePrefix>/models/item";
import Offer from "<modulePrefix>/models/offer";
Second, your moduleForModel('item') should have needs: ['model:offer'].
Here's a passing a test using the provided files. (I used: ember new stackoverflow)
import Ember from "ember";
import { test, moduleForModel } from 'ember-qunit';
/* Import Models */
import Item from "stackoverflow/models/item";
import Offer from "stackoverflow/models/offer";
moduleForModel('item', 'Item', {
// Item needs the offer model.
needs: ['model:offer']
});
test('offer relationship', function() {
/* For some reason this was necessary to prime the store. */
/* Without this line I get the error:
'undefined' is not an object (evaluating 'store.modelFor') */
var model = this.subject();
/* App.Item -> Item, App.Offer -> Offer */
var relationships = Ember.get(Item, 'relationships');
deepEqual(relationships.get(Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});
Related
THE ROUTE
import Ember from "ember";
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';
const {
Route,
} = Ember;
export default Route.extend(UnauthenticatedRouteMixin, {
});
THE TEST
import { moduleFor, test } from 'ember-qunit';
moduleFor('route:index', 'Unit | Route | index', {});
test('it exists', function(assert) {
var route = this.subject();
assert.ok(route);
});
THE ERROR
Promise rejected before it exists: Could not find module `ember-simple-auth/mixins/unauthenticated-route-mixin` imported from `appname/routes/index`
Ember: 1.13.15
Ember Simple Auth: 1.0.1
It is just the standard test generated by ember but fails everytime.
Thank you for any help
I'm trying to use the Ember Validations addon and I can't get it working. In the Chrome console, I see: WARNING: Could not find the "presence" validator.
Here is my model:
import Ember from 'ember';
import DS from 'ember-data';
import EmberValidations from 'ember-validations';
export default DS.Model.extend(EmberValidations.Mixin, {
name: DS.attr('string'),
validations: {
name: {
presence: true
}
}
});
And here is the my test:
import Ember from 'ember';
import EmberValidations from 'ember-validations';
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('person', 'Unit | Model | person', {
// Specify the other units that are required for this test.
needs: ['ember-validations#validator:local/presence'],
afterEach: function() {
window.sessionStorage.clear();
}
});
test('isValid should be false if name is not set', function(assert) {
stop();
var model = this.subject();
console.log(model);
Ember.run(function() {
sinon.spy(model, 'save');
model.validate().then(function() {
start();
assert.equal(model.get('isValid'), false);
});
});
});
The result of this test is:
Died on test #1 at Object.test (http://localhost:4200/assets/test-support.js:1644:11)
at http://localhost:4200/assets/myproj.js:14450:15
at mod.state (http://localhost:4200/assets/vendor.js:150:29)
at tryFinally (http://localhost:4200/assets/vendor.js:30:14)
at requireModule (http://localhost:4200/assets/vendor.js:148:5)
at Object.TestLoader.require (http://localhost:4200/assets/test-loader.js:29:9)
at Object.TestLoader.loadModules (http://localhost:4200/assets/test-loader.js:21:18): <(unknown mixin):ember848>
You need to add dependencies to test suite as defined in ember-validations docs in testing part. However, take notice that these docs are a little bit outdated. The proper needs should include only the validators you use (presence) and look like this:
needs: ['ember-validations#validator:local/presence']
Do not indclude service:validations.
I posted an issue quite a while ago but it's not discussed yet.
I am using ember cli with ember data and have been piecing together information but it still doesn't work. This all involves the Home model, route and template. I feel like I'm close but still no cigar. I took everything out except for the title to simplify it. According to documentation I've read, everything is as it should be.
here is my app.js
import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
import config from './config/environment';
Ember.MODEL_FACTORY_INJECTIONS = true;
var App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver
});
loadInitializers(App, config.modulePrefix);
export default App;
Here is my home model:
import DS from 'ember-data';
export default DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
Here is my home route:
import Ember from 'ember';
//import DS from 'ember-data';
export default Ember.Route.extend({
model: function(){
return this.store.find('Home');
},
});
This is my home template:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
{{outlet}}
Could someone please help me out?
I think it has something to do with my model hook.
Could this also be a controller issue. I generated a basic controller. Should I have generated an arrayController?. The home route when saved gives me this error message:
models/home.js: line 9, col 1, 'Home' is not defined. but when I define it there still is a problem. Am I supposed to ad a folder called adapters, then put a file in it called application.js, then ad export default DS.FixtureAdapter.extend(); . But when I do that it tells me DS is not defined
It looks like you are explicitly exporting your model as Home and then you are trying to find('home') which does not have the same letter case.
Ember will not automatically resolve the case for you. If you want to use Home as the model, you will need to call it in the same way every time.
You might even need to import Home from '../models/model-file'; if you want to call it from the route..
An easier thing to try would be to use the implicit export default Ember.Model.extend({}) and let ember-cli resolve the class using your file name.
http://www.ember-cli.com/#using-modules
Your template seems to be the issue. You reference an item property that isn't there:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
Also, you have some syntax problems with your model. Try this:
import DS from 'ember-data';
var Home= DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
export default Home;
For the Fixture Adapter you are correct in adding the application.js to the adapters folder and import DS from 'ember-data'.
Also in Steve H's example of your home model, the definition of the FIXTURES is not correct. Try using:
Home.FIXTURES = [
...
];
Make sure the pathing is correct.
Where/how can i adjust the Ember.Inflector Class / create an instance of it that ember-cli picks up?
Thanks!
I generated an initializer and put this data there. This ensures it loads before anything that might need it. Like the model, adapter, or serializer.
initializers/inflector.js
import Ember from 'ember';
export function initialize(/* container, application */) {
var inflector = Ember.Inflector.inflector;
inflector.uncountable('aamc-pcrs');
}
export default {
name: 'inflector',
initialize: initialize
};
I placed it in the model file and it worked fine:
import DS from 'ember-data';
import Ember from 'ember';
var inflector = Ember.Inflector.inflector;
inflector.irregular('nota', 'notas');
inflector.singular(/nota/, 'nota');
export default DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
language: DS.attr('string'),
body: DS.attr('string')
});
The Ember Guides covers this in Models - Customizing Adapters:
Create the file app/models/custom-inflector-rules.js:
import Inflector from 'ember-inflector';
const inflector = Inflector.inflector;
inflector.irregular('formula', 'formulae');
inflector.uncountable('advice');
// Meet Ember Inspector's expectation of an export
export default {};
Then in app/app.js add the line:
import './models/custom-inflector-rules';
And if you want to use this in a unit test for a serializer/adapter then you can just import the custom-inflector-rules file into the test.
I am trying to test a model relationship in an ember-cli application but it keeps telling me: No model was found for 'rateType'. It appears that it can't find my models.
Files
~app/models/account.js
~app/models/rate-type.js
Account Model
export default DS.Model.extend({
...
rateType: DS.belongsTo('rateType'),
});
Test
import Ember from 'ember';
import { test, moduleForModel } from 'ember-qunit';
import Account from 'app/models/account';
import RateType from 'app/models/rate-type';
moduleForModel('account', 'Account Model', {
// Specify the other units that are required for this test.
needs: ['model:rate-type']
});
test('rateType relationship', function() {
expect(0);
this.subject(); //error here
// var relationships = Ember.get(Account, 'relationships');
// deepEqual(relationships.get('rate-type'), [
// { name: 'rateType', kind: 'belongsTo' }
// ]);
});
I have tried camel casing the needs attribute butit does not like that at all.
needs: ['model:rateType', 'model:fuelGroup']
I think what you need is the needs keyword:
moduleForModel('post', 'Unit | Model | post', {
needs: ['model:comment', 'model:user']
});
I found it in the docs here: http://guides.emberjs.com/v1.10.0/testing/testing-models/
Your issue is with the model. Try dasherizing 'rate-type' in the belongsTo relationship.
export default DS.Model.extend({
...
rateType: DS.belongsTo('rate-type')
});