服务器

Mirage 服务器。

请注意,this 在您的 routes 函数中指的是服务器实例,它与您在测试中所指的 server 实例相同。

属性

db: any

返回 Mirage Db 实例。

inflector: any

Mirage 需要知道某些单词的单数和复数形式,以便其某些 API 正常工作。

例如,每当您定义一个模型时

new Server({
  models: {
    post: Model
  }
})

Mirage 将把单词 "post" 变成复数,并用它创建一个 db.posts 数据库集合。

为了实现这一点,Mirage 使用了一个称为 Inflector 的对象。Inflector 是一个包含两个方法的对象,即 singularizepluralize,Mirage 将在需要对单词进行变形时调用这两个方法。

Mirage 有一个默认的 Inflector,因此如果您编写

new Server()

您将使用 node inflected 包。如果您有非标准词语或需要更改默认值,可以自定义它。您可以在 自定义变形的指南 中了解更多信息。

通常,您应该能够使用提供的 Inflector 进行自定义。与后端使用的任何自定义变形保持一致,这样可以使 Mirage 代码更一致、更简单。

您还可以完全覆盖 Inflector 并提供您自己的 pluralizesingularize 方法

new Server({
  inflector: {
    pluralize(word) {
      // your logic
    },
    singularize(word) {
      // your logic
    }
  }
})

logging: any

设置为 truefalse 以明确指定日志记录行为。

默认情况下,服务器响应会在非测试环境中记录。在测试中,日志记录默认情况下是禁用的,以免干扰 CI 测试运行程序的输出。

例如,要启用测试中的日志记录,请编写以下内容

test('I can view all users', function() {
  server.logging = true;
  server.create('user');

  visit('/users');
  // ...
});

您还可以使用 Pretender 服务器的 handledRequest 钩子 编写自定义日志消息。(您可以通过 server.pretender 从 Mirage 服务器访问 Pretender 服务器。)

要覆盖,请

new Server({
  routes() {
    this.pretender.handledRequest = function(verb, path, request) {
      let { responseText } = request;
      // log request and response data
    }
  }
})

namespace: any

设置使用 getpostputdel 定义的所有路由的基本命名空间。

例如,

new Server({
  routes() {
    this.namespace = '/api';

    // this route will handle the URL '/api/contacts'
    this.get('/contacts', 'contacts');
  }
})

请注意,只有在 this.namespace 之后定义的路由才会受到影响。如果您有一些不希望在命名空间下的临时路由,这将很有用

new Server({
  routes() {

    // this route handles /auth
    this.get('/auth', function() { ...});

    this.namespace = '/api';
    // this route will handle the URL '/api/contacts'
    this.get('/contacts', 'contacts');
  };
})

如果您的应用程序是从文件系统而不是服务器加载的(例如,通过 Cordova 或 Electron 而不是 localhosthttps://yourhost.com/),您需要显式地定义命名空间。可能的取值是 /(如果请求使用相对路径发出)或 https://yourhost.com/api/...(如果请求发出到已定义的服务器)。

有关利用配置的 API 主机和命名空间的示例实现,请查看 此问题评论

pretender: any

Mirage 使用 pretender.js 作为其 xhttp 拦截器。在 Mirage 配置中,this.pretender 指的是实际的 Pretender 实例,因此在那里有效的任何配置选项在这里也同样有效。

new Server({
  routes() {
    this.pretender.handledRequest = (verb, path, request) => {
      console.log(`Your server responded to ${path}`);
    }
  }
})

如果您想更改 Pretender 实例上的任何选项,请参考 Pretender 文档

schema: any

返回 Mirage 模式(ORM)实例。

timing: any

设置服务器响应时间的毫秒数。

默认情况下,在开发期间延迟 400 毫秒,在测试中延迟 0 毫秒(以便您的测试快速运行)。

new Server({
  routes() {
    this.timing = 400; // default
  }
})

要设置单个路由的计时,请查看路由处理程序的 timing 选项。

urlPrefix: any

设置一个字符串,用于为所有路由处理程序 URL 添加前缀。

如果您的应用程序向不同端口发出 API 请求,这将很有用。

new Server({
  routes() {
    this.urlPrefix = 'https://#:8080'
  }
})

方法

create(type: any, traitsAndOverrides: any): any

生成单个 type 类型的模型,将其插入数据库(并赋予其 ID),并返回已添加的数据。

test("I can view a contact's details", function() {
  let contact = server.create('contact');

  visit('/contacts/' + contact.id);

  andThen(() => {
    equal( find('h1').text(), 'The contact is Link');
  });
});

您可以使用作为第二个参数传入的哈希表来覆盖工厂定义中的属性。例如,如果我们有以下工厂

export default Factory.extend({
  name: 'Link'
});

我们可以像这样覆盖名称

test("I can view the contacts", function() {
  server.create('contact', {name: 'Zelda'});

  visit('/');

  andThen(() => {
    equal( find('p').text(), 'Zelda' );
  });
});

createList(type: any, amount: any, traitsAndOverrides: any): any

创建 amounttype 类型的模型,可以选择使用 attrs 覆盖工厂中的属性。

返回已添加到数据库中的记录数组。

以下是一个来自测试的示例

test("I can view the contacts", function() {
  server.createList('contact', 5);
  let youngContacts = server.createList('contact', 5, {age: 15});

  visit('/');

  andThen(function() {
    equal(currentRouteName(), 'index');
    equal( find('p').length, 10 );
  });
});

以下是一个来自设置开发数据库的示例

new Server({
  seeds(server) {
    let contact = server.create('contact')

    server.createList('address', 5, { contact })
  }
})

loadFixtures(...args: String): any

默认情况下,如果您没有定义工厂,fixtures 将在测试期间加载;如果您没有定义 seeds,它将在开发期间加载。您可以使用 loadFixtures() 在这两种环境中加载 fixture 文件,除了使用工厂来填充您的数据库之外。

server.loadFixtures() 加载所有文件,而 server.loadFixtures(file1, file2...) 加载选择的 fixture 文件。

例如,在测试中,您可能希望从加载所有 fixture 数据开始

test('I can view the photos', function() {
  server.loadFixtures();
  server.createList('photo', 10);

  visit('/');

  andThen(() => {
    equal( find('img').length, 10 );
  });
});

或者在开发中,您可能希望加载一些参考 fixture 文件,并使用工厂来定义您的其余数据

new Server({
  ...,
  seeds(server) {
    server.loadFixtures('countries', 'states');

    let author = server.create('author');
    server.createList('post', 10, {author_id: author.id});
  }
})

passthrough(...paths: String, options: Array): any

默认情况下,如果您的应用程序发出在服务器配置中未定义的请求,Mirage 将抛出错误。您可以使用 passthrough 来将请求列入白名单,并允许它们通过 Mirage 服务器传递到实际的网络层。

注意:将所有 passthrough 配置放在路由的底部,以便路由处理程序优先。

要忽略当前主机(以及配置的 namespace)上的路径,请使用前导 /

this.passthrough('/addresses');

您也可以传递路径列表,或者多次调用 passthrough

this.passthrough('/addresses', '/contacts');
this.passthrough('/something');
this.passthrough('/else');

这些行将允许所有 HTTP 动词通过。如果您希望只有某些动词通过,请将包含指定动词的数组作为最后一个参数传递

this.passthrough('/addresses', ['post']);
this.passthrough('/contacts', '/photos', ['get']);

您可以将函数传递给 passthrough 来对是否应由 Mirage 处理请求进行运行时检查。如果函数返回 true,Mirage 不会处理请求,而是让它通过。

this.passthrough(request => {
  return request.queryParams.skipMirage;
});

如果您希望当前域上的所有请求都通过,只需在不带参数的情况下调用该方法

this.passthrough();

再次注意,当前命名空间(即此调用之上定义的任何 namespace 属性)将被应用。

您也可以允许其他来源主机通过。如果您使用完全限定的域名,namespace 属性将被忽略。使用两个 * 通配符来匹配路径下的所有请求

this.passthrough('http://api.foo.bar/**');
this.passthrough('http://api.twitter.com/v1/cards/**');

在 0.12 之前的 Pretender 版本中,passthrough 仅适用于 jQuery >= 2.x。只要您使用的是 Pretender@0.12 或更高版本,您就可以正常使用。

shutdown(): any

关闭服务器并停止拦截网络请求。