1

我正在使用 Ember-konacha-rails 来测试我的 ember.js 应用程序,

这是我正在测试的模型:

//sales_rep.js

App.SalesRep = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
});

这是测试:

 #= require spec_helper

describe "App.SalesRep", ->
  beforeEach( ->
    Test.store = TestUtil.lookupStore()
  )

  it "is a DS.Model", ->
    assert.ok App.SalesRep
    assert.ok DS.Model.detect(App.SalesRep)

  describe "attribute: firstName", ->
    it "can be created with valid value", ->
      Ember.run( ->
        Test.contact = App.SalesRep.createRecord firstName: 'Joe'
      )
      expect(Test.contact.get 'firstName').to.equal 'Joe'




    describe "attribute: lastName", ->
        it "can be created with valid value", ->
          Ember.run( ->
            Test.contact = App.SalesRep.createRecord lastName: 'swanson'
          )
          expect(Test.contact.get 'lastName').to.equal 'swanson'


        it "can NOT be created with invalid value", ->
          Ember.run( ->
            Test.contact = App.SalesRep.createRecord firstName: '123'
          )
          expect(Test.contact.get 'firstName').to.not.equal 'Joe'
          expect(Test.contact.isValid).to.be.equal false

属性测试通过,但expect(Test.contact.isValid).to.be.equal false失败,这是错误:

    can NOT be created with invalid value ‣
AssertionError: expected undefined to equal false
    at Assertion.assertEqual (http://localhost:3500/assets/chai.js:862:12)
    at Assertion.ctx.(anonymous function) [as equal] (http://localhost:3500/assets/chai.js:3048:25)
    at Context.<anonymous> (http://localhost:3500/assets/app/models/sales_rep_spec.js?body=1:36:51)
    at Test.Runnable.run (http://localhost:3500/assets/mocha.js:4065:32)
    at Runner.runTest (http://localhost:3500/assets/mocha.js:4430:10)
    at http://localhost:3500/assets/mocha.js:4476:12
    at next (http://localhost:3500/assets/mocha.js:4356:14)
    at http://localhost:3500/assets/mocha.js:4365:7
    at next (http://localhost:3500/assets/mocha.js:4313:23)
    at http://localhost:3500/assets/mocha.js:4333:5

因此,我一般是 JS 新手,我不知道如何解决这个问题?是测试语法错误吗?

还是应该向 Ember 模型添加验证?

4

1 回答 1

1

尝试:

expect(Test.contact.get('isValid')).to.be.equal false

基本上添加 .get('isValid')

于 2013-08-28T14:22:18.303 回答