2015-08-03 112 views
0

我想设置为我的灰烬CLI与Ember CLI Mirage(1.13.1)验收测试模拟服务器。我被困在如何调试设置并实际测试视图中可用的模型数据。调试灰烬CLI Mirage在验收测试

我尝试添加我的海市蜃楼路线内的控制台日志声明:

this.get('/users', function(db){ 
    console.log(db.users); 
    return db.users; 
}); 

告诉我,海市蜃楼路线被调用,应该有三个用户存在。但我的测试仍然失败。如何查看我的验收测试或我的模板中的商店内容?

测试/接受/用户/索引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: expected 0 to equal 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> 

回答

3

我通常通过查看的数据选项卡开始Ember Inspector查看是否有任何模型被添加到商店。

如果你在1.13,你可能使用JSON API接口,需要做更多的只是有点工作在你的海市蜃楼路由处理,像一个类型的数据项下返回对象。

例如,它可能是这样的:

this.get('/users', function(db){ 
    return { 
    data: db.users.map(u => ({ 
     id: u.id, 
     type: u.type, 
     attributes: _.omit(u, ['id', 'type']) 
    })) 
    }; 
}); 

请注意,你的工厂仅用于播种幻影的分贝。因此,与上面的路线,你现在会能够使用工厂像你在你的问题中定义

// mirage/scenarios/default.js 
export default function(server) { 
    server.createList('user', 10); 
}); 

,然后当你启动你的应用程序之一,它做了一个GET请求来/users,数据应由Ember Data返回并正确地反序列化。

+0

是的,我正在使用'JSONAPIAdapter'。因为我以前使用过Sinon.fakeServer并根据JSONAPI设置了响应,所以很有意义。 – max

+0

感谢您的快速响应! – max

+0

所以我需要把工厂记录,并通过像[JSONAPISerializer](http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html)? – max