伺服器 (Server)
Mirage 伺服器。
請注意,在您的 routes
函式中,this
指的是伺服器實例,也就是在您的測試中 server
指向的同一個實例。
屬性
db: any
傳回 Mirage 資料庫 (Db) 實例。
inflector: any
Mirage 需要知道某些詞的單數和複數形式,才能使其某些 API 運作。
例如,每當您定義一個模型
new Server({
models: {
post: Model
}
})
Mirage 會將 "post" 這個詞複數化,並用它來建立一個 db.posts
資料庫集合。
為了實現這一點,Mirage 使用一個名為 Inflector 的物件。Inflector 是一個物件,它有兩個方法:singularize
和 pluralize
,Mirage 會在需要變換詞形時呼叫這些方法。
Mirage 有一個預設的 inflector,所以如果您寫
new Server()
您將會使用 node inflected 套件。如果您有不規則的詞彙或需要更改預設值,則可以自訂此設定。您可以在自訂詞形變化的指南中了解更多資訊。
您通常應該能夠使用提供的 inflector 進行自訂。最好讓您的自訂詞形變化與您的後端使用的任何詞形變化相符,因為這會使您的 Mirage 程式碼更加一致和簡單。
您也可以完全覆寫 inflector,並提供您自己的 pluralize
和 singularize
方法
new Server({
inflector: {
pluralize(word) {
// your logic
},
singularize(word) {
// your logic
}
}
})
logging: any
設定為 true
或 false
以明確指定記錄行為。
預設情況下,伺服器回應會記錄在非測試環境中。在測試中,預設會停用記錄,以避免使 CI 測試執行器輸出雜亂無章。
例如,若要在測試中啟用記錄,請寫入以下內容
test('I can view all users', function() {
server.logging = true;
server.create('user');
visit('/users');
// ...
});
您也可以使用 Pretender 伺服器的 handledRequest
hook 來撰寫自訂記錄訊息。(您可以透過 server.pretender
從您的 Mirage 伺服器存取 pretender 伺服器。)
若要覆寫,
new Server({
routes() {
this.pretender.handledRequest = function(verb, path, request) {
let { responseText } = request;
// log request and response data
}
}
})
namespace: any
設定所有使用 get
、post
、put
或 del
定義之路由所使用的基礎命名空間。
例如,
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,而不是 localhost
或 https://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: 任何
設定伺服器回應時間的毫秒數。
預設情況下,開發期間會有 400 毫秒的延遲,而測試時則無延遲 (以便您的測試快速執行)。
new Server({
routes() {
this.timing = 400; // default
}
})
若要為個別路由設定時間,請參閱路由處理器的 timing
選項。
urlPrefix: 任何
設定一個字串,以作為所有路由處理器 URL 的前綴。
如果您的應用程式向不同的埠發出 API 請求,這會很有用。
new Server({
routes() {
this.urlPrefix = 'https://127.0.0.1:8080'
}
})
方法
create(type: 任何, traitsAndOverrides: 任何): 任何
產生單一 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: 任何, amount: 任何, traitsAndOverrides: 任何): 任何
建立 amount 個 type 類型的模型,並可選擇使用 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: 字串): 任何
預設情況下,如果您沒有定義工廠,則在測試期間會載入 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: 字串, options: 陣列): 任何
預設情況下,如果您的應用程式發出未在伺服器設定中定義的請求,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(): 任何
關閉伺服器並停止攔截網路請求。