我正在尝试使用Ember CLI Mirage为我的 Ember CLI (1.13.1) 验收测试设置一个模拟服务器。我坚持如何调试设置并实际测试视图中可用的模型数据。
我尝试在 Mirage 路由中添加控制台日志语句:
this.get('/users', function(db){
console.log(db.users);
return db.users;
});
这告诉我海市蜃楼路由被调用,应该有三个用户在场。但我的测试仍然失败。如何在验收测试或模板中检查商店中的内容?
测试/接受/用户/index-test.js
/* jshint expr:true */
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import { expect } from 'chai';
import Ember from 'ember';
import startApp from 'tagged/tests/helpers/start-app';
describe('Acceptance: UsersIndex', function() {
var application;
var users;
beforeEach(function() {
application = startApp();
users = server.createList('user', 3);
});
afterEach(function() {
Ember.run(application, 'destroy');
});
it('can visit /users/index', function() {
visit('/users');
andThen(function() {
expect(currentPath()).to.equal('users.index');
});
});
it('lists the users', function(){
visit('/users');
andThen(function() {
users = server.createList('user', 3);
expect(find('.user').length).to.equal(3); // fails
});
});
});
AssertionError:预期 0 等于 3
应用程序/海市蜃楼/config.js
export default function() {
/*
Config (with defaults).
Note: these only affect routes defined *after* them!
*/
this.namespace = '/api/v1'; // make this `api`, for example, if your API is namespaced
// this.timing = 400; // delay for each request, automatically set to 0 during testing
this.get('/users');
}
// You can optionally export a config that is only loaded during tests
export function testConfig() {
this.timing = 1;
}
应用程序/海市蜃楼/工厂/user.js
import Mirage, {faker} from 'ember-cli-mirage';
export default Mirage.Factory.extend({
email: function(){ return faker.internet.email(); }
});
应用程序/路由/用户/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('user');
}
});
应用程序/模板/用户/index.hbs
<h2>Users</h2>
<table>
<thead>
<tr>
<th>Actions</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{{#each model as |user|}}
<tr class="user">
<td class="actions"><a href="#">Show</a></td>
<td class="email">{{ user.email }}</td>
</tr>
{{/each}}
</tbody>
</table>