如何使用this.get('store')单元测试控制器 [英] how to unit test controller which uses this.get('store')

查看:166
本文介绍了如何使用this.get('store')单元测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用摩卡单位测试我的控制器。我的控制器看起来像:

  AS.MyController = Ember.ObjectController.extend(Ember.Validations.Mixin,{

名称:null,
描述:null,

init:function(){
this._super();

this。 get('store')find('something');
},
....
});

我的测试开始如下:

  describe(MyControllerTest,function(){
//尝试但没有工作
//删除AS.MyController.init;
var控制器= AS.MyController.create();
.....
})

浏览器总是在init中的this.get('store')调用中抛出错误。我不知道我是否需要存储东西,或者有一个工作,因为我的测试用例根本不依赖于存储。在任何一种情况下,我找不到很多东西,真的很感激任何反馈。谢谢,Dee



JSBIN http://jsbin.com/aMASeq/3/



更新:
可以有很多方法来解决这个问题,但是我最后所做的是通过将所有的函数调用存储到单独的动作中,然后在init中来重构控制器代码我使用this.send('actioName')调用这些动作函数。在我的单元测试中,在实例化控制器之前,我重新打开控制器来修改这些动作功能(它更容易改变操作功能,而不是改变init函数本身,当尝试改变init时,我总是遇到一些js错误)。例如:

  AS.MyController.reopen({actions:{setSomeActionThatUsesStore:function(){
// do something这不涉及使用商店
}}});


解决方案

控制器从容器访问商店。您可以创建一个模拟容器并使用它来实例化控制器。

  var mockContainer = new Ember.Container(); 
mockContainer.register('store:main',Ember.Object.extend({
find:function(){...}
});

var controller = App.PostController.create({container:mockContainer});

如果您需要访问真正的商店然后你可以从你的应用程序的容器中抓取控制器。

  var controller = App .__ container __。lookup('controller:post'); 

一个 PostController 为您拥有连接在一起的所有依赖关系(如商店)。


I am unit testing my controller using mocha. My controller looks like:

AS.MyController = Ember.ObjectController.extend(Ember.Validations.Mixin, {

    name: null,
    description: null,

    init: function () {
        this._super();

        this.get('store').find('something');
    },
    ....
}); 

And my test begins like:

describe("MyControllerTest", function () {
    //tried but didn't work
    //delete AS.MyController.init;
    var controller = AS.MyController.create();
    .....
})  

and the browser always throws error on "this.get('store')" call in init. I am not sure if I need to stub things out or there is a work around for it because my test case doesn't rely on store at all. In either case, I couldn't find much out there and would really appreciate any feedback.

Thanks, Dee

JSBIN : http://jsbin.com/aMASeq/3/

UPDATE : There can be many ways to tackle this issue, but what I ended up doing is re-structuring the controller code a bit by putting all the function calls to store into separate actions and then in init I make calls to these action functions using this.send('actioName'). In my unit test, before instantiating the controller, I reopen the controller to modify these action functions(its easier to change action function than to change init function itself, when trying to change init I always got into some js error). Eg:

AS.MyController.reopen({actions: {setSomeActionThatUsesStore: function () {
         //do something that doesn't involve using store
        }}});

解决方案

Controllers get access to the store from the container. You can create a mock container and instantiate the controller with it.

var mockContainer = new Ember.Container();
mockContainer.register('store:main', Ember.Object.extend({ 
  find: function() { ... }
});

var controller = App.PostController.create({ container: mockContainer });

If you need access to the real store then you can just grab the controller from your App's container.

var controller = App.__container__.lookup('controller:post');

That will instantiate a PostController for you that has all of it's dependencies (such as store) wired together.

这篇关于如何使用this.get('store')单元测试控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆