简体   繁体   English

ember-cli-mirage错误:处理程序返回任何内容,但处理程序存在

[英]ember-cli-mirage error: Nothing returned by handler, but handler exists

In the app I'm working with, we have a GET route that validates a user's email address. 在我正在使用的应用程序中,我们有一个GET路由来验证用户的电子邮件地址。 If the email is invalid, the server responds with: 如果电子邮件无效,服务器将响应:

  • a 200 status code 200状态代码
  • response headers with Content-Type:application/json; charset=utf-8 Content-Type:application/json; charset=utf-8响应头Content-Type:application/json; charset=utf-8 Content-Type:application/json; charset=utf-8
  • and the response data itself is just a string of "This email is invalid" 并且响应数据本身只是一串“此电子邮件无效”

I'm trying to simulate this in ember-cli-mirage by doing: 我试图通过以下方式在ember-cli-mirage模拟这个:

this.get('/ember_api/v1/validations/validate_email', function() {
  return [200, { 'Content-Type': 'application/json' }, "This email is invalid"];

  // also tried this:
  // return new Mirage.Response(200, { 'Content-Type': 'application/json' }, JSON.stringify({"message":"This email is invalid"}));

  // and tried this:
  // return "This email is invalid";
});

The test itself is a button click that fires off this request: 测试本身是一个按钮点击,它会触发此请求:

GET "/ember_api/v1/validations/validate_email?email=fakey%40fakefakefake.com&skip_uniq=true"

...and the error I'm getting is: ...而我得到的错误是:

Pretender intercepted GET /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true but encountered an error: Nothing returned by handler for /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true. 伪装者拦截GET /ember_api/v1/validations/validate_email?email=tom%40gmail.com&skip_uniq=true但遇到错误:/ember_api / v1 / invalidations / validate_email?email = tom%40gmail.com&skip_uniq = true的处理程序没有返回任何内容。 Remember to return [status, headers, body]; 记得返回[状态,标题,正文]; in your route handler.` 在你的路线处理程序中

It's asking me to return [status, headers, body] , but I'm doing this in my handler, and it still throws the error. 它要求我return [status, headers, body] ,但我在我的处理程序中这样做,它仍然会抛出错误。

Is this actually an issue with the response? 这实际上是响应的问题吗? Do I need to edit my API to actually return a JSON API formatted object, so I can write the test that way? 我是否需要编辑我的API以实际返回JSON API格式的对象,以便我可以这样编写测试?

I feel like I should be able to return a string in my test since that's what the app is doing. 我觉得我应该能够在测试中返回一个字符串,因为这就是应用程序正在做的事情。 Any help is appreciated. 任何帮助表示赞赏。

The this.get you are using is the Mirage version. 你正在使用的this.get是Mirage版本。 You can also use this.pretender.get which should work with your current code sample ... 您也可以使用this.pretender.get ,它应该与您当前的代码示例一起使用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM