问题
I am trying to make use of the api-stubs provided in ember-app-kit for testing.
The stubs work correctly when I view the site in the browser, and when I run the tests by visting /tests
in the browser.
However, when I run the tests from the command line with testem (by running grunt test:server
) the tests fail as the stub data isn't returned.
You can find a minimal example demonstrating the problem here: https://github.com/tomclose/minimal_eak_test_stub_problem, created from the most recent version of ember-app-kit.
What am I doing wrong?
回答1:
Because the testem stuff runs on port (7239 is it?) you a) can't run the apistub server ont he same port (already bound).
Thus, you have to run the normal app (port 8000), let testem run on its own port.. and allow testem via the 'host' param in an adapter go find the right host:port combination to actually get data.
As well, the addition of the cors() filter will make the :8000 test server willing to send data back to a "cross domain request" from something on port 7239
The use of sinon is a different case (don't want to use apistub at all). I actually settled on NOT trying to use apistub for testing, but certainly the above approach works great.
回答2:
rstudner contacted me on irc and helped me to solve this problem. The basic idea is to keep a grunt server
running and to use config/environments/test.js
to configure your test api requests to hit this server.
Here's what worked:
Change
app/adapters/application.js
to point towards a configurable host:export default DS.RESTAdapter.extend({ namespace: 'api', host: window.ENV.host });
Edit
config/environments/test.js
to configure tests to hit the instance of your app running asgrunt server
:window.ENV.host = 'http://localhost:8000'
You then run into CORS problems. To solve these, add the dependency
"cors": "2.2.0",
topackage.json
and then runnpm install
. Then ..In
tasks/express-server.js
on line 8 add:var cors = require('cors');
and on line 25 add:
app.use(cors());
You can find an updated working version at https://github.com/tomclose/minimal_eak_test_stub_problem.
rstudner also mentioned that he actually uses sinon.js
to fake the server 100% of the time (and then uses selenium for some full-stack tests), rather than following this approach.
来源:https://stackoverflow.com/questions/23080436/api-stubs-failing-when-running-tests-with-testem-in-ember-app-kit