鉴于Ember最近达到了1.0.0,我想开始使用它进行测试.我正在使用Yeoman 1.0和Karma.我想单元测试模型,但我发现很难完成隔离.
我现在的例子是:
describe("Expense",function() { return it("has a computed property called `explained`",function() { var expense = App.Expense.create({ name: "My first expense",value: 34 }); return expect(expense.get("explained")).to.equal("My first expense -- 34"); }); });
从1.0.0开始,我收到以下错误:
Error: You should not call `create` on a model. Instead,call `store.createRecord` with the attributes you would like to set.
我应该如何访问商店以创建模型实例?更理想的是,如何在不诉诸商店的情况下简单地生成这样的模型,这是否可行?为了测试IMO模型,产生整个应用程序毫无意义.
谢谢.
解决方法
这是我到目前为止用于单元测试模型的最小代码.
var container,store,expense; container = new Ember.Container(); container.register('store:main',DS.Store.extend()); container.register('model:expense',App.Expense); store = container.lookup('store:main'); Ember.run( function() { expense = store.createRecord('expense',{ name: "My first expense",value: 34 }); });
根据商店的代码和the way models are tested inside Ember Data,我认为你不能减少测试的设置.