i have problem with retreving data in unit test from store by service method. Here is my code:
This is my test:
import { moduleFor, test } from 'ember-qunit';
import startMirage from '../../helpers/setup-mirage-for-integration';
moduleFor('service:filter-by-ingredients', 'Unit | Service | filter by ingredients', {
needs: ['model:recipe', 'model:category'],
beforeEach() {
startMirage(this.container);
},
afterEach() {
window.server.shutdown();
}
});
test('should find recipe with title', async function(assert) {
let recipe = server.create('recipe', {
categories: server.createList('category', 5),
})
let service = this.subject();
let result = await service.findRecipesWithContent();
assert.equal(result.get('firstObject').get('categories').get('firstObject'), 10);
});
This is my service:
import Service from '@ember/service';
import { inject as service } from '@ember/service'
export default Service.extend({
store: service(),
findRecipesByWithContent() {
return this.get('store').findAll('recipe');
}
});
My mirage config:
export default function() {
this.get('/recipes', function(schema){
return schema.recipes.all();
});
this.get('/categories', function(schema){
return schema.categories.all();
});
this.post('/recipes', (schema, request) => {
const attrs = JSON.parse(request.requestBody);
return schema.recipes.create(attrs);
});
this.post('/categories', (schema, request) => {
const attrs = JSON.parse(request.requestBody);
return schema.categories.create(attrs);
});
}
My factories:
import { Factory, faker, hasMany } from 'ember-cli-mirage';
export default Factory.extend({
title: faker.list.random('omelette', 'chicken with salad', 'toasts', 'risotto', 'penne', 'spaghetti'),
portionAmount: faker.list.random(1, 2, 3, 4, 5, 6),
preparationTime: faker.list.random(5, 10, 15, 30, 45, 60),
description: 'this is the description',
calories: faker.list.random(100, 150, 200, 300, 400, 500),
categories: hasMany('category'),
rating: faker.list.random(1, 2, 3, 4, 5, 6),
});
Category factory
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
name: faker.list.random('dinner', 'breakfast', 'vegan', 'vegetarian', 'supper'),
});
And finally my models:
category
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr()
});
recipes
import DS from 'ember-data';
import { hasMany } from 'ember-data/relationships';
export default DS.Model.extend({
title: DS.attr(),
portionAmount: DS.attr(),
preparationTime: DS.attr('number'),
description: DS.attr(),
calories: DS.attr('number'),
categories: hasMany('category', { async: true }),
rating: DS.attr(),
});
If i print:
console.log(server.db.recipes)
It shows all recipes with categories, one in my case, but when i call
service method findRecipeWithContent(), result.get('firstObject').get(categories).get('length')
is 0. I can't get to this relationships.
If anyone can help me i would be very gratefull. Greetings!
Aucun commentaire:
Enregistrer un commentaire