问题
I am using ember-cli-mirage for acceptance tests. For a specific case, I would like to check the behaviour while fetching data over a slow connection.
There's a setting in ember-cli-mirage called timing
that simulates a delay in the response. However, this setting cannot be changed to be different in a specific test:
// app/mirage/config.js
this.timing = 400;
Something else I have tried is returning a promise at the fake endpoint. Through some import/export, I could control the resolution of the promise from my test. Unfortunately, ember-cli-mirage doesn't seem to recognise the return value as a promise, and simply passes it back to the adapter verbatim:
// app/mirage/config.js
this.get('/StopPoint/Search/:term', (db, request) => {
return freezer.run(function() {
return db[`stop-point-search-${request.params.term}`][0];
});
});
// At my test
freezer.on()
runTests()
freezer.off()
The question: is there any way to do this? Ie: to control the delay of a specific response in ember-cli-mirage?
回答1:
A few thoughts:
You can change timing within a specific test via
server.timing
. The server should be reinstantiated for each test so this wouldn't affect other tests.test('for slow behavior', function() { server.timing = 400; // });
You can also redefine route handlers within tests as shown here in the second example of the Acceptance testing guides. If you're using
0.2.0-beta
, route handlers have atiming
option you can use to affect just that handler:test('for slow behavior', function() { server.get('/slow-query', (schema, request) => { // return data; }, {timing: 400}; visit('/'); // assert(); });
I think your instinct to return something you have control over freezing would be the ideal way to test this, in tandem with something like Timecop. Perhaps Mirage can add an API for this eventually.
来源:https://stackoverflow.com/questions/35812351/introduce-momentary-delays-in-ember-cli-mirage