我已经设置了一个ember.js应用程序,我使用的是ember.js 1.0.0-rc4和ember.js 0.13,我试图用类似于这个example的mocha.js来设置konacha。
我的spec_helper.js
//= require konacha_config
//= require_tree ./templates
//= require application_test
//= require sinon
//= require spec_utils
// Sinon fake server
var server;
// Stub out Konacha.reset()
Konacha.reset = Ember.K;
// Prevent automatic scheduling of runloops. For tests, we
// want to have complete control of runloops.
Ember.testing = true;
// Defer App readiness (it will be advanced in each test below)
App.deferReadiness();
// Prevent the router from manipulating the browser's URL.
App.Router.reopen({location: 'none'});
beforeEach(function(done) {
// Fake XHR
server = sinon.fakeServer.create();
Ember.run(function() {
// Advance Contagion readiness, which was deferred above.
App.advanceReadiness();
// Setup is complete when the Contagion readiness promise resolves
App.then(function() {
done();
});
});
});
afterEach(function() {
// Reset App state
App.reset();
// Restore XHR
server.restore();
});我所拥有的规格正在运行和传递,但在铬控制台中,我看到的东西如下
x GET http://localhost:3500/posts 404 (Not Found)
x GET http://localhost:3500/comments 404 (Not Found)为什么sinon假服务器不排除这些请求?
我试过这样的事情
server.respondWith("GET", "/comments",
[200, { "Content-Type": "application/json" },
'{"commemnts":[{"id":1,"text":"Comment 1"},{"id":2,"text":"Comment 2"}]}'
]);"/comments.json","http://localhost:3500/comments和"http://localhost:3500/comments.json的url的变化
似乎什么都起不到作用。
我也尝试过使用sinon.stub(App.Comments,"find")来解决查找方法,但是我仍然看到了404错误。
您知道出了什么问题吗?或者用正确的方式来模拟/存根这些请求并返回有意义的json?
更新1
当我设置server.autoRespond = true时
未知错误:假XHR onreadystatechange处理程序抛出异常:断言失败:您已打开测试模式,从而禁用了运行循环的自动运行。您将需要在Ember.run中包装任何具有异步副作用的代码。
即使所有东西都封装在一个Ember.run中,也会发生这种情况。
将server.respond()添加到afterEach函数会导致相同的假XHR错误。
添加
Ember.run(function(){
server.respond();
});afterEach函数将我带着404错误返回到1
发布于 2013-06-19 13:54:35
您可以将服务器设置为自动响应server.autoRespond = true;或使用server.respond();触发服务器响应。否则,服务器将得到请求,但什么也不做。
https://stackoverflow.com/questions/17191822
复制相似问题