在验收测试

时间:2015-11-13 21:18:32

标签: ember.js model acceptance-testing

我正在尝试编写一个验收测试,以查看我访问的路径模型中的某个属性是否等于我所声称的内容。

我没有使用此路线向页面输出信息,而是使用ember插件将其中的一部分保存到localstorage。所以通常我意识到我可以使用find()在页面上查找元素并检查它的内容以确定模型是否正在被解析但是对于这种情况不起作用。

在验收测试中我有这个设置(使用海市蜃楼btw)

test('Returns a user', function(assert) {
  // Generate a user
  var user = server.create('user',{first_name: 'Jordan'});
  // Visit the index page with the users short_url
  visit('/' + user.short_url);
  var route = this.application.__container__.lookup('route:index');

  // Assert that the model the user we created by checking the first name we passed in
  assert.equal(route.model.first_name,'Jordan','Model returns user with first name Jordan');
});

但是当我运行测试时,它会将结果显示为undefined

更新

在尝试Daniel Kmak的回答后,我仍然无法通过。这是我正在使用的路线代码

import Ember from 'ember';
import LocalUser from 'bidr/models/user-local';

export default Ember.Route.extend({
    localUser: LocalUser.create(),
    navigationService: Ember.inject.service('navigation'),
    activate() {
        this.get('navigationService').set('navigationMenuItems', []);
    },
    beforeModel() {
        this.localUser.clear();
    },
    model(params) {
        var self = this;
        return this.store.queryRecord('user',{short_url: params.short_url}).then(function(result){
            if(result){
            self.set('localUser.user', {
                "id": result.get('id'),
                "first_name": result.get('first_name'),
                "active_auction": result.get('active_auction'),
                "phone": result.get('phone')
            });
            // transition to event page
            self.transitionTo('items');
            } else {
                self.transitionTo('home');
            }
        });
    }
});

测试看起来像这样

import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'bidr/tests/helpers/start-app';


module('Acceptance | index route', {
  beforeEach: function() {
    this.application = startApp();
  },

  afterEach: function() {
    Ember.run(this.application, 'destroy');
  }
});

test('Returns a user', function(assert) {
  var user = server.create('user',{first_name: 'Jordan'});

  visit('/' + user.short_url);

  var route = this.application.__container__.lookup('route:index');

  andThen(function() {
    assert.equal(route.get('currentModel.first_name'),'Jordan','Model returns user with first name Jordan');
  });
});

所有代码都在开发中运行。

1 个答案:

答案 0 :(得分:1)

好的,所以我已经在Ember中进行了测试,看起来你应该很高兴在andThen钩子中获得模型:

test('returns a user', function(assert) {
  visit('/'); // visit your route

  var route = this.application.__container__.lookup('route:index'); // find your route where you have model function defined

  andThen(function() {
    console.log(route.get('currentModel')); // your model value is correct here
    assert.equal(currentURL(), '/'); // make sure you've transitioned to correct route
  });
});

拿你的代码应该运行得很好:

test('Returns a user', function(assert) {
  var user = server.create('user',{first_name: 'Jordan'});

  visit('/' + user.short_url);

  var route = this.application.__container__.lookup('route:index');

  andThen(function() {
    assert.equal(route.get('currentModel.first_name'),'Jordan','Model returns user with first name Jordan');
  });
});

另外需要注意的是,您可以通过route.currentModel属性访问模型。

对于我的模特:

export default Ember.Route.extend({
  model() {
    return Ember.RSVP.hash({
      simple: 'simpleValue',
      promise: Ember.RSVP.resolve(5)
    });
  }
});

andThen console.log(route.get('currentModel'));我得到了:

Object {simple: "simpleValue", promise: 5}

记录。

相关问题