api-stubs failing when running tests with testem in ember-app-kit

二次信任 提交于 2019-12-11 03:48:56

问题


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:

  1. Change app/adapters/application.js to point towards a configurable host:

    export default DS.RESTAdapter.extend({
      namespace: 'api',
      host: window.ENV.host
    });
    
  2. Edit config/environments/test.js to configure tests to hit the instance of your app running as grunt server:

    window.ENV.host = 'http://localhost:8000'
    
  3. You then run into CORS problems. To solve these, add the dependency "cors": "2.2.0", to package.json and then run npm install. Then ..

  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!