使用ember-cli-mirage测试错误响应 [英] Testing error responses with ember-cli-mirage

查看:227
本文介绍了使用ember-cli-mirage测试错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读ember-cli-mirage的关于创建模拟响应的文档,但是无法确定如何针对完全相同的请求来测试错误响应。例如:

  test(我可以查看用户,function(){
var users = server。 createList('user',3);

访问('/ users');

和Then(function(){
等于(find ).length,3);
equal(find('li:first')。text(),users [0] .name);
});
});

test(如果查看用户返回错误,我可以查看错误,function(){
//以某种方式设置错误响应(?)

访问('/ users');

andThen(function(){
等于(find('#error')。length,1);
});
});

看起来像在路由中形成响应的唯一方法

  this.get('/ users',function(db,request){

if(something based on the request,i猜猜?){
return new Mirage.Response(500,{},{message:'Oops!Something bad happenned。:('});
} else {
return db.users .insert([
{name:'Zelda',age:142},
{name:'Epona',age:58},
]);
}
$);

海市age楼如何推荐这样做?

解决方案

在测试中,加载了 config.js 中定义的路由处理程序,但由于您有访问权限到服务器,你实际上可以覆盖那些处理程序。



在这种情况下我所做的只是创建一个ad-hoc错误状态的路由处理程序:

  test(我可以查看错误,如果viewin g用户返回错误,function(){
server.get('/ users',{errors:['有一个错误']},404);

访问('/ users');

andThen(function(){
equal(find('#error')。length,1);
});
});

由于每个测试都重新启动服务器,因此此处理程序将不会存在于其他测试中。 p>

还有一个 PR 用于允许您编写瞬时路由处理程序的API,这将有助于测试应用程序可以从错误中恢复。


I'm reading through ember-cli-mirage's docs about creating mock responses but can't figure out how to test error responses for the exact same request. For example:

test("I can view the users", function() {
  var users = server.createList('user', 3);

  visit('/users');

  andThen(function() {
    equal( find('li').length, 3 );
    equal( find('li:first').text(), users[0].name );
  });
});

test("I can view the error if viewing the users returns an error", function() {
  // somehow set up an error response (?)   

  visit('/users');

  andThen(function() {
    equal( find('#error').length, 1 );
  });
});

It looks like the only way to form the response is in the route

this.get('/users', function(db, request) {

    if (something based on the request, i guess?) {
      return new Mirage.Response(500, {}, {message: 'Oops! Something bad happenned. :('});
    } else {
        return db.users.insert([
            {name: 'Zelda', age: 142},
            {name: 'Epona', age: 58},
        ]);
    }
});

How does mirage recommend going about doing this?

解决方案

Within tests, the route handlers defined in config.js are loaded, but since you have access to server you can actually overwrite those handlers.

What I do in this situation is just create an ad-hoc route handler for the error state:

test("I can view the error if viewing the users returns an error", function() {
  server.get('/users', {errors: ['there was an error']}, 404);

  visit('/users');

  andThen(function() {
    equal( find('#error').length, 1 );
  });
});

Since the server is reinstantiated for each test, this handler won't exist in other tests.

There's also a PR for an API that would allow you to write up transient route handlers, which would be useful for testing that your application could recover from an error.

这篇关于使用ember-cli-mirage测试错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆