Pretender encountered an error:
arenoir opened this issue · 6 comments
I am upgrading my application from ember 2.18 to 3.4 and running into the following error.
Error: Pretender intercepted POST /api/v2/customers but encountered an error: Nothing returned by handler for /api/v2/customers. Remember to `return [status, headers, body];` in your route handler.
Every test where I am using the fails method is returning the same error. Here is the test:
test('saveChangeset assigns errors if server returns error', function(assert) {
assert.expect(4);
const done = assert.async();
const customer = run(() => this.owner.lookup('service:store').createRecord('customer', {
name: 'bad name'
}));
mockCreate('customer').match(
(requestData) => {
return requestData.name === 'bad name';
}
).fails(
{
status: 422,
response: {
errors: {
name: ['is already taken']
}
}
}
);
mockCreate('customer').match(
(requestData) => {
return requestData.name === 'all good now';
}
).returns({attrs: {name: 'all good now'}});
run(() => {
customer.saveChangeset().catch((e) => {
let error = customer.get('changeset.error.name.validation')
assert.equal(customer.get('changeset.isValid'), false);
assert.equal(error, 'is already taken');
}).catch(done);
});
next(() => {
changeset.set('name', 'all good now');
customer.saveChangeset().then(() => {
assert.equal(changeset.get('isValid'), true);
assert.equal(customer.get('name'), 'all good now');
done();
}).catch(done);
});
});
It seems to be an issue with the match method. All mocks using a match method are returning the error.
mockCreate('job').match(
{
address_id: "10"
}
).returns(
{
attrs: {
id: 1,
name: 'new job name',
address_id: 10,
customer_id: customer.id
}
}
);
Error: Pretender intercepted POST /api/v2/jobs but encountered an error: Nothing returned by handler for /api/v2/jobs. Remember to `return [status, headers, body];` in your route handler.
While removing the match works.
mockCreate('job').returns(
{
attrs: {
id: 1,
name: 'new job name',
address_id: 10,
customer_id: customer.id
}
}
);
there are so many things here to improve .. where do i start ??
test('saveChangeset assigns errors if server returns error', async function(assert) {
assert.expect(4);
const customer = makeNew('customer', {name: 'bad name'});
const mockCreateCustomer = mockCreate(customer);
mockCreateCustomer.match({name: 'bad name'})
.fails(
{
status: 422,
response: {
errors: {
name: ['is already taken']
}
}
}
);
try {
await customer.saveChangeset();
} catch (e) {
let error = customer.get('changeset.error.name.validation')
assert.equal(customer.get('changeset.isValid'), false);
assert.equal(error, 'is already taken');
}
mockCreateCustomer.match({name: 'all good now'}).succeeds();
changeset.set('name', 'all good now');
await customer.saveChangeset();
assert.equal(changeset.get('isValid'), true);
assert.equal(customer.get('name'), 'all good now');
});
try that
also, i have had issue with match before , and it takes a bit of time sometimes to track it down, but in your case you were making 2 mocks for the same create and not reusing it properly .. so your setup was not correct in the first place.
word of note. you are not really using factory guy to its fullest potential .. maybe that is fault of docs, but when you find your self doing stuff like:
const customer = run(() => this.owner.lookup('service:store').createRecord('customer', {
name: 'bad name'
}));
your should have a bell ring in your brain telling you ( i am doing something dumb right now )
because factory guy can make all models and their assications for you .. no need for that hackery
So it looks like the match function requestData is a different object.
mockCreate('customer').match(
(requestData) => {
return requestData.name === 'all good now';
}
)
needed to be changed to:
mockCreate('customer').match(
(requestData) => {
return requestData.customer.name === 'all good now';
}
)
i disagree with this:
mockCreate('customer').match(
(requestData) => {
return requestData.customer.name === 'all good now';
}
)
but if that makes you happy .. have at it
@danielspaniel thanks for the help. These are old tests and in need of some cleanup.
I am changing the match function to use the attribute hash. But the previous function was working with ember-data 2.18 and ember-data-factory-guy 2.16.
got it @arenoir , hope you take my advice though about reusing that mockCreate and so on, because it will help alot. if you need any other assistance, just let me know